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

Git for Windows无法读取SSH配置,始终使用默认密钥

解决Git始终默认使用~/.ssh/id_rsa的问题

你的核心问题是克隆命令没用到SSH config里定义的Host别名,导致SSH不会匹配对应的密钥配置,只会默认查找id_rsa。

问题原因

你写的SSH config里定义了account1、account2两个Host别名,但克隆时用的是git@github.com:account2/my-repo.git——这里的主机名是github.com,和config里的Host别名不匹配,SSH自然不会加载对应的IdentityFile,只会走默认的密钥查找逻辑。

解决方法

方法1:用SSH config的Host别名克隆

直接把克隆命令里的github.com换成你config里的Host别名,比如针对account2的仓库:

git clone git@account2:account2/my-repo.git

这样SSH会匹配到Host account2块,自动使用对应的C:\Users\Me\.ssh\github\id_rsa_account2密钥。

方法2:调整SSH config的Host匹配规则

如果习惯用github.com的写法,可以给每个账户加带后缀的Host,比如:

Host github.com-account1
    Hostname github.com
    User git
    IdentityFile C:\Users\Me\.ssh\github\id_rsa_account1

Host github.com-account2
    Hostname github.com
    User git
    IdentityFile C:\Users\Me\.ssh\github\id_rsa_account2

然后克隆时用:

git clone git@github.com-account2:account2/my-repo.git

验证配置是否生效

用SSH测试连接,确认密钥是否正常使用:

ssh -T account2

如果返回Hi account2! You've successfully authenticated, but GitHub does not provide shell access.,说明配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:07:38