我在我的工作电脑上,它已经 [成功] 配置为连接到我们的 GitHub 帐户并分别使用 SSH 和 GPG key 验证我们的提交。在我开始改变之前,我的原始 ~/.ssh/config
文件是这样的:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
为了添加我的个人帐户,我生成了一个新的 SSH key (~/.ssh/id_rsa_personal
),将 .pub
部分添加到我的个人 GitHub 帐户,然后修改我的~/.ssh/config
文件如下:
# Default
Host *
AddKeysToAgent yes
UseKeychain yes
# Personal
Host github.com-PERSONAL
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
# Work
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
此更改后,我仍然可以毫无问题地与我的工作帐户进行交互——一切都没有改变。但是,当我尝试使用
与我的个人帐户进行交互时git clone git@github-PERSONAL:nikblanchet/myrepository.git
,我收到一条错误消息:
Cloning into 'myrepository'...
ssh: Could not resolve hostname github-personal: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
为了缩小问题范围,我决定尝试简单的 SSH 身份验证
ssh -T git@github.com-PERSONAL
,令人惊讶的是,它奏效了!
Hi nikblanchet! You've successfully authenticated, but GitHub does not provide shell access.
(按预期运行 ssh -T git@github.com
通过我的工作帐户进行身份验证。)
所以现在我迷路了。为什么 ssh -T
可以解析主机名而 git clone
不能?我错过了什么?
最佳答案
您的网址应该是:
github.com-PERSONAL:nikblanchet/myrepository.git
你先试过
git@github-PERSONAL:nikblanchet/myrepository.git
这就是它无法解析 github-PERSONAL
的原因,它不在您的配置文件中。
github.com-PERSONAL
是。
注意:无需添加 git@
部分:您的配置文件已经指定了 User git
。
关于authentication - GitHub 多个帐户 : Can authenticate SSH with both accounts, 但无法克隆一个存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212303/