多GitHub账户SSH配置异常:推送John账户仓库代码失败求助
多GitHub账户SSH配置推送失败问题描述
此前仅使用James账户的GitHub仓库,新增John账户后,按以下步骤配置SSH密钥,但推送John账户仓库时出错
已完成的操作:
- 为James生成SSH密钥对:
ssh-keygen -t ed25519 -C "jamesemailaddress@example.com" -f james_key
添加到SSH代理:ssh-add ~/.ssh/james_key - 为John重复密钥生成流程:
ssh-keygen -t ed25519 -C "johnsemailaddress@example.com" -f johns_key
添加到SSH代理:ssh-add ~/.ssh/johns_key - 配置
~/.ssh/config文件:# James GitHub account Host github.com-james HostName github.com User git IdentityFile ~/.ssh/james_key # John GitHub account Host github.com-john HostName github.com User git IdentityFile ~/.ssh/johns_key - 已将两个账户的公钥分别添加至对应GitHub账户的SSH Keys区域
- 为James生成SSH密钥对:
遇到的错误:
ERROR: Repository not found. fatal: Could not read from remote repository.
Please make sure you have the correct access rights额外验证:在John仓库执行
ssh -T git@github.com,返回认证James账户成功,说明SSH优先使用了James的配置
解决方法
1. 修改John仓库的远程地址
当前John仓库的远程地址还是默认的git@github.com:John/repo-name.git,未匹配到你为John配置的SSH Host规则,需要替换为对应的别名:
git remote set-url origin github.com-john:John/你的仓库名.git
验证修改结果:
git remote -v
正确输出示例:
origin github.com-john:John/你的仓库名.git (fetch) origin github.com-john:John/你的仓库名.git (push)
2. 验证John账户的SSH连接
使用配置的Host别名测试认证:
ssh -T git@github.com-john
返回Hi John! You've successfully authenticated, but GitHub does not provide shell access.即为认证成功。
3. 可选:清理SSH代理缓存(若密钥冲突)
如果仍存在认证混淆,先清除代理中的所有密钥,再重新添加:
ssh-add -D ssh-add ~/.ssh/james_key ssh-add ~/.ssh/johns_key
关键说明
SSH会根据~/.ssh/config中的Host字段匹配规则选择密钥,直接使用git@github.com会触发默认密钥匹配(优先使用James的密钥),只有使用git@github.com-john时,才会调用John的密钥配置。
内容的提问来源于stack exchange,提问作者ololo
相关产品推荐
相关产品推荐

