如何在SSH密钥之间切换以在github帐户中使用



我有两个SSH密钥。一个是我公司的github账户,另一个是我个人的github账户。在我为我的个人帐户创建另一个SSH密钥之前,我的公司帐户SSH密钥一直工作良好。我创建的最后一个SSH密钥对是为我的个人帐户创建的,它运行良好。现在,我正试图推送到我的公司帐户,但我得到一个错误消息。是否有某种方法可以使用两个SSH密钥或在它们之间切换?我将分享我的配置文件。第一个是我公司帐户的原始SSH密钥。

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes

Git FAQ详细介绍了这个主题,包括SSH和HTTPS。对于SSH, FAQ建议在您的~/.ssh/config中执行以下操作:

# This is the account for author on git.example.org.
Host example_author
HostName git.example.org
User git
# This is the key pair registered for author with git.example.org.
IdentityFile ~/.ssh/id_author
IdentitiesOnly yes
# This is the account for committer on git.example.org.
Host example_committer
HostName git.example.org
User git
# This is the key pair registered for committer with git.example.org.
IdentityFile ~/.ssh/id_committer
IdentitiesOnly yes

然后提示你需要更改url:

然后,你可以调整你的推送URL使用git@example_authorgit@example_committer而不是git@example.org(例如,git remote set-url git@example_author:org1/project1.git)。

显然,在您的情况下,您将希望使用github.com来代替git.example.org,并且您可能希望为主机名使用不同的条目。