同一台设备上的多个 SSH 密钥

Multiple SSH Keys on the same device

我几个月前注册了个人 SSH 密钥,一切正常。现在我在一家公司工作。他们创建了自己的 GitHub 帐户,我已经启动了一个新的存储库。

我知道我必须添加另一个 SSH 密钥,我已经这样做了。

这是 ~/.ssh/config 文件的内容。

Host github.com
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

Host github-companyname
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_companyname

我也做了 ssh <keyname> 并且我通过了身份验证。

之后我执行了以下命令。

git init
git add 
git remote add origin <repo>

一切正常,直到我 运行 git push -u origin master.

我收到这个错误。

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

我不明白。一切似乎都设置正确。

为什么我会收到该错误消息?

如果这有什么不同,我正在使用 OSX Sierra 10.12.4。

由于我的评论解决了 OP 问题,所以我将此作为答案写下来。

问题似乎出在同一主机上有多个 ssh 密钥。在您的 .ssh/config 中,您配置了 2 台主机 - github.comgithub-companyname。在您的公司存储库中,您需要将 .git/config 中的远程 url 从 git@github.com:... 更改为 git@github-companyname:...。然后 ssh 将使用正确的密钥,您应该不会遇到身份验证问题。

进一步阅读:

当您需要使用公司密钥克隆现有存储库时,可以应用相同的方法。

git clone git@github-companyname:companyname/repositoryname.git

请注意,该命令使用 github-companyname.

而不是 github.com