要使用 git clone
命令克隆 GitHub 仓库 itwanger/paicoding.git
,请按以下步骤操作:
1. 确保已安装 Git
在终端CMD运行:
git --version
如果未安装,请先下载安装:Git 官网
2. 克隆仓库
git clone git@github.com:itwanger/paicoding.git
这会在当前目录下创建名为 paicoding
的文件夹,并将代码下载到其中。
克隆在哪个盘,就在哪个盘输入命令。
遇到问题
D:\>git clone git@github.com:itwanger/paicoding.git
Cloning into 'paicoding'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
因为第一次连接github.com时,Git没有该主机的记录,所以需要用户确认是否继续。
根据提示,我们需要输入"yes"来接受该主机的指纹,并将其添加到已知主机列表中(~/.ssh/known_hosts)。
因此,我们应该在提示符后输入"yes"(不带引号),然后按回车。
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
出现此错误是因为:
-
本地未生成 SSH 密钥
-
SSH 密钥未添加到 GitHub 账户
-
SSH 代理未运行或未加载密钥
完整解决步骤
检查 SSH 密钥是否存在:
打开 PowerShell 或 CMD:
dir $env:USERPROFILE\.ssh\
结果出现:C:\Users\ycx>dir $env:USERPROFILE\.ssh\ 文件名、目录名或卷标语法不正确。
如果显示 "找不到文件",说明您还没有 SSH 密钥。
创建 .ssh 目录(如果不存在):
if not exist "%USERPROFILE%\.ssh" mkdir "%USERPROFILE%\.ssh"
如果看不到 id_ed25519
或 id_rsa
文件,需要生成新密钥
1. 生成新的 SSH 密钥(如果尚未生成)
ssh-keygen -t ed25519 -C "your_email@example.com"
-
按 3 次回车接受默认设置(不要设置密码)
-
将
your_email@example.com
替换为你的 GitHub 邮箱
2. 将公钥添加到 GitHub 账户
# 查看并复制公钥内容
cat ~/.ssh/id_ed25519.pub
复制输出的全部内容(以 ssh-ed25519 AAA...
开头的内容)
-
登录 GitHub → Settings → SSH and GPG keys
-
点击 "New SSH Key"
-
粘贴复制的公钥内容
-
点击 "Add SSH Key"
3. 创建 SSH 配置文件
notepad $env:USERPROFILE\.ssh\config
粘贴以下内容:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
保存并关闭
4. 测试 SSH 连接
ssh -T git@github.com
当提示时输入 yes
,应该看到:
5. 重新克隆仓库
git clone git@github.com:itwanger/paicoding.git
验证关键文件:
dir "%USERPROFILE%\.ssh"
应该看到:
-
id_ed25519
(私钥) -
id_ed25519.pub
(公钥) -
config
(配置文件)