You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境GitLab SSH克隆失败求助:SSH验证成功但克隆报错

解决Windows下Git克隆GitLab仓库权限拒绝问题

1. 统一Git使用的SSH客户端路径

Windows自带的OpenSSH和Git内置的OpenSSH可能存在冲突,先检查当前Git配置的SSH命令:

git config --global core.sshCommand

如果返回空或路径不明确,将其设置为系统自带的SSH(或Git内置的,二选一即可):

  • 使用Windows系统SSH:
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"
  • 使用Git内置SSH(默认安装路径示例):
git config --global core.sshCommand "C:/Program Files/Git/usr/bin/ssh.exe"

2. 确保ssh-agent加载了正确密钥

  • 以管理员权限打开PowerShell,启动并设置ssh-agent为自动启动:
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
  • 添加Windows下的SSH私钥到agent:
ssh-add ~/.ssh/id_rsa  # 替换为你的私钥文件名,如id_ecdsa、id_ed25519等

若提示无法连接到认证代理,先执行以下命令再重新添加:

eval $(ssh-agent -s)

3. 强制指定GitLab对应的密钥

在Windows的C:\Users\你的用户名\.ssh目录下新建或修改config文件,添加以下内容,强制Git访问GitLab时使用指定密钥:

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile C:\Users\你的用户名\.ssh\id_rsa  # 替换为你的私钥绝对路径
  IdentitiesOnly yes

IdentitiesOnly yes会避免ssh-agent尝试其他无关密钥,减少权限验证冲突。

4. 验证Git的SSH连接细节

临时开启Git的SSH verbose模式,查看连接时的密钥加载情况:

git config --global core.sshCommand "ssh -v"
git ls-remote git@gitlab.com:你的用户名/目标仓库.git

在输出中确认加载的私钥与你上传到GitLab的公钥匹配,测试完成后关闭verbose模式:

git config --global --unset core.sshCommand

5. 检查仓库远程URL格式

确保克隆或拉取使用的是SSH格式URL(git@gitlab.com:xxx/xxx.git),而非HTTPS格式。若之前用的是HTTPS,可修改远程URL:

git remote set-url origin git@gitlab.com:你的用户名/目标仓库.git

内容的提问来源于stack exchange,提问作者stephendgb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 12:39:56