无法获取多个SSH密钥以用于多个github帐户 [英] Can't get Multiple SSH keys to work for multiple github accounts
问题描述
我正在尝试为两个不同的github帐户设置多个ssh密钥,但我似乎无法按照我的想法开始工作。
目前我有两个ssh密钥,我们称它们为 a_rsa
,用于从github.com-a和 b_rsa
克隆的repos从$ github.com-b克隆的回购站
我在Ubuntu上,我的所有密钥和配置位于〜/ .ssh
。我的ssh配置如下所示:
#Account A
Host github.com-a
HostName github .com
用户git
IdentityFile〜/ .ssh / a_rsa
#帐户b
主机github.com-b
主机名github.com
User git
IdentityFile〜/ .ssh / b_rsa
.git / config
文件是:
url = git @ github.com:a/SomeRepo.git
无论我尝试推送哪种回购或从主人拉它总是尝试使用密钥b_rsa。
我已经尝试更改每个用户到 a
和 b
分别无效。我不确定我的配置是否被读取,因为我已经尝试将我的配置文件设置为这样:
主机*
IdentityFile〜/ .ssh / a_rsa
我所有的回购仍然尝试使用 b_rsa
键。
我希望它的工作方式是基于回购协议的所有者,推到(所有者 a
或 b
)它会使用适当的键,但我似乎无法弄清楚什么是错误的。
问题出在储存库的网址。
您应该使用git存储库URL中的 Host
,而不是<$ c $中的主机名
c>〜/ .ssh / config
因此,您的存储库中的URL .git / config
应该是
url = git@github.com-a:a / SomeRepoForUserA.git
OR,
url = git @ gi thub.com-b:b / SomeRepoForUserB.git
在进行提交时使用正确的Github身份
如果这些存储库的用户名和电子邮件地址不同,您想要做的另一件事是,然后提交这些存储库,你应该使用相应的用户名,电子邮件像这样:
git -c user.name =UserA-c user.email=UserA@blah.com commit -m提交到存储库A
AND,
git -c user.name =UserB-c user.email=UserB@blah.com commit -mCommit做到存储库B
修正了不正确的用户名,电子邮件
如果您已经对不正确的作者和用户进行了提交,应该使用它来解决它。
git -c user.name =UserA-c user.email=UserA@blah.com commit --amend --reset-author
当然,上面的修复只是最近的提交。如果存在较旧的提交,可以使用 来结合上面的命令OR交互式rebase( git rebase -i
>) .com / questions / 4493936 / could-i-change-my-name-and-surname-in-all-before-commits> filter-branch
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.
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.
I'm on Ubuntu and all my keys and config are located in ~/.ssh
. My ssh config looks like this:
#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:
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.
I have tried changing the User on each to a
and b
respectively to no avail. 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.
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.
The problem is in the URL for the repositories.
You should be using the Host
in the git repository URL and not the Hostname
from the ~/.ssh/config
Therefore, the URL in your repositories .git/config
should be either
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
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
这篇关于无法获取多个SSH密钥以用于多个github帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!