简体   繁体   English

无法获取多个SSH密钥以用于多个github帐户

[英]Can't get Multiple SSH keys to work for multiple github accounts

I am trying to set up multiple ssh keys for two different github accounts but I can't seem to get it to work like I'd like. 我正在尝试为两个不同的github帐户设置多个ssh密钥,但是我似乎无法像我想要的那样使它工作。

Currently I have two ssh keys, we'll call them a_rsa for repos cloned from github.com-a and b_rsa for repos cloned from github.com-b. 目前我有两个SSH密钥,我们会打电话给他们a_rsa从github.com-a和克隆回购b_rsa从github.com-B克隆回购。

I'm on Ubuntu and all my keys and config are located in ~/.ssh . 我在Ubuntu上,我所有的键和配置都位于~/.ssh My ssh config looks like this: 我的ssh配置看起来像这样:

#Account A
Host github.com-a
    HostName github.com
    User git
    IdentityFile ~/.ssh/a_rsa

#Account b
Host github.com-b
    HostName github.com
    User git
    IdentityFile ~/.ssh/b_rsa

and an example url in one of my .git/config files is: 我的.git/config文件之一中的示例网址是:

url = git@github.com:a/SomeRepo.git

No matter which repo I am in when I try to push or pull from master it always tries to use key b_rsa. 不管我在哪个回购中,当我尝试从母版推入或拉出时,它总是尝试使用键b_rsa。

I have tried changing the User on each to a and b respectively to no avail. 我尝试将每个用户分别更改为ab无济于事。 I'm not sure if my config is even being read as I have tried setting my config file to just this: 我不确定是否正在读取我的配置,因为我尝试将配置文件设置为:

Host *
    IdentityFile ~/.ssh/a_rsa

And all my repos still try to use the b_rsa key. 而且我的所有存储库仍尝试使用b_rsa键。

The way I would like it to work is based on the owner of the repo I am tyring to push to (owner a or b ) it would use the appropriate key but I can't seem to figure out what is wrong. 我希望它的工作方式基于我要推送到的回购协议的所有者(所有者ab ),它将使用适当的密钥,但我似乎无法弄清楚出了什么问题。

The problem is in the URL for the repositories. 问题出在存储库的URL中。

You should be using the Host in the git repository URL and not the Hostname from the ~/.ssh/config 您应该在git存储库URL中使用Host ,而不要使用~/.ssh/configHostname

Therefore, the URL in your repositories .git/config should be either 因此,存储库.git/config的URL应该是

url = git@github.com-a:a/SomeRepoForUserA.git

OR, 要么,

url = git@github.com-b:b/SomeRepoForUserB.git

Using the correct Github identity while making commits 在提交时使用正确的Github身份

The other thing you would want to do, if the username and email address for these repositories are different, then while making commits to these repositories, you should use the corresponding username, email like so: 您要做的另一件事是,如果这些存储库的用户名和电子邮件地址不同,则在提交这些存储库时,您应使用相应的用户名,电子邮件,如下所示:

git -c user.name="UserA" -c user.email=UserA@blah.com commit -m "Commit done to repository A"

AND, 和,

git -c user.name="UserB" -c user.email=UserB@blah.com commit -m "Commit done to repository B"

Fixing commits with incorrect username, email 使用错误的用户名,电子邮件修复提交

If you have already made commits with the incorrect author and user, this should be used to fix it. 如果您已经使用错误的作者和用户进行了提交,则应使用它来修复它。

 git -c user.name="UserA" -c user.email=UserA@blah.com commit --amend --reset-author

Ofcourse, the above fixes only the most recent commit. 当然,以上内容仅修复了最新的提交。 If there are older commits you could an interactive rebase ( git rebase -i ) combined with the above command OR, use filter-branch 如果存在较旧的提交,则可以将交互式基础( git rebase -i )与上述命令结合使用,或者,使用filter-branch

Try with 试试看

IdentitiesOnly yes

IdentitiesOnly Specifies that ssh(1) should only use the authentication identity files configured in the ssh_config files, even if ssh-agent(1) offers more identities. IdentitiesOnly指定即使ssh-agent(1)提供更多身份,ssh(1)也应仅使用ssh_config文件中配置的身份验证身份文件。 The argument to this keyword must be ''yes'' or ''no''. 此关键字的参数必须为“是”或“否”。 This option is intended for situations where ssh-agent offers many different identities. 该选项适用于ssh-agent提供许多不同身份的情况。 The default is ''no''. 默认为“否”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM