初步现象是使用git-SSH拉取代码的时候提示下面的错误:

1
2
3
4
5
6
7
git clone git@gitee.com:Doocs/md.git
Cloning into 'md'...
git@gitee.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测试连接情况:

1
2
3
4
5
6
7
 ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.76.198.77)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi cuianbing(@cuianbing)! You've successfully authenticated, but GITEE.COM does not provide shell access.

从你的操作输出可以看到,SSH 连接测试明明显示认证成功(Hi cuianbing(@cuianbing)! You've successfully authenticated),但克隆仓库时依然报公钥权限拒绝,这是一个典型的SSH 密钥生效但 Git 未正确使用该密钥的问题。

使用调试模式检查具体问题:

1
2
3
4
5
6
7
8
9
10
11
cuianbing@DESKTOP-2BDNTTN:~/.ssh$ GIT_SSH_COMMAND="ssh -vvv" git clone git@gitee.com:cuianbing/no.git
Cloning into 'no'...
debug1: OpenSSH_10.0p2 Debian-7, OpenSSL 3.5.4 30 Sep 2025
debug3: Running on Linux 6.6.114.1-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Mon Dec 1 20:46:23 UTC 2025 x86_64
.........// 省略大量日志
debug3: send packet: type 1
Transferred: sent 2748, received 5980 bytes, in 0.3 seconds
Bytes per second: sent 9835.9, received 21404.3
debug1: Exit status 0
Receiving objects: 100% (7/7), done.
Resolving deltas: 100% (1/1), done.

你发现的这个现象非常关键:用 GIT_SSH_COMMAND="ssh -vvv" git clone 能正常克隆,但直接执行 git clone 却失败,这说明默认情况下 Git 调用的 SSH 环境和你手动指定的 SSH 环境不一致,核心问题是 Git 没有使用正确的 SSH 配置 / 密钥。

问题本质分析

GIT_SSH_COMMAND="ssh -vvv" git clone 这个命令的作用是:强制 Git 使用系统默认的 ssh 命令(并开启详细日志)来执行克隆,而直接 git clone 时,Git 可能调用了其他版本的 SSH(比如 Git for Windows 自带的 ssh,而非系统的 OpenSSH),或者没有加载你的密钥配置。

解决方案(按优先级)

方案 1:永久配置 Git 使用正确的 SSH 程序

这是最根本的解决方法,让 Git 始终使用能正常工作的 ssh 程序:

1
2
3
4
5
6
7
8
9
# 1. 先查看系统默认的 ssh 路径(就是你执行 ssh -T 成功的那个 ssh)
which ssh
# 输出示例:/usr/bin/ssh 或 /bin/ssh

# 2. 配置 Git 全局使用这个 ssh 程序
git config --global core.sshCommand "/usr/bin/ssh" # 替换为上一步查到的路径

# 3. 现在直接克隆即可
git clone git@gitee.com:Doocs/md.git

方案 2:简化版 - 给 Git 配置固定的 SSH 密钥参数

如果方案 1 无效,直接给 Git 配置强制使用你的密钥,避免每次手动加参数:

1
2
3
4
5
6
7
8
# 配置 Git 全局使用指定的密钥(替换为你的私钥路径)
git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes"

# 验证配置是否生效
git config --global core.sshCommand

# 直接克隆
git clone git@gitee.com:Doocs/md.git

方案 3:通过 SSH 配置文件永久生效(推荐)

结合之前的 SSH config 配置,确保所有对 Gitee 的 SSH 连接都用正确的密钥:

1
2
# 编辑 SSH 配置文件
vim ~/.ssh/config

粘贴以下内容(确认 IdentityFile 是你的私钥路径):

1
2
3
4
5
6
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey
IdentitiesOnly yes # 关键:强制只使用指定的密钥

保存退出(ESC:wq)后,直接执行克隆命令即可。

方案 4:临时解决 - 把参数加入环境变量

如果你只是临时需要,不想改全局配置,可以把 GIT_SSH_COMMAND 加入环境变量:

1
2
3
4
5
# Linux/Mac 临时生效(当前终端)
export GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes"

# 直接克隆
git clone git@gitee.com:Doocs/md.git

如果想永久生效,把上面的 export 命令加到你的 shell 配置文件(~/.bashrc~/.zshrc),然后执行 source ~/.bashrc 生效。

验证是否彻底解决

执行以下命令,确认无需额外参数也能克隆:

1
2
3
4
5
# 先删除之前克隆成功的 md 目录(可选)
rm -rf md

# 直接克隆
git clone git@gitee.com:Doocs/md.git

如果输出 Cloning into 'md'... 并开始下载,说明问题解决。

总结

  1. 核心原因:Git 默认调用的 SSH 程序 / 配置和你手动测试的不一致,加 GIT_SSH_COMMAND 后强制使用了正确的 SSH 环境;
  2. 最佳解决方案:用 git config --global core.sshCommand 配置 Git 全局使用正确的 SSH 程序,或配置 ~/.ssh/config 强制 Gitee 用指定密钥;
  3. 关键参数:IdentitiesOnly=yes 能避免多密钥冲突,确保只使用你指定的私钥。