更多存储库的git公钥

时间:2014-06-10 07:25:48

标签: git github

我遇到了以下问题。

我有2个项目,我使用github。第一个项目进展顺利,我创建了一个公钥,输入密码并推入github。没问题。

第二个项目,我创建了另一个公钥,当我想推送到第二个存储库时,它不断要求第一个代码/.ssh/id_rsa的密码。但是id_rsa拥有我第一个项目的公钥。所以当然,当我输入密码时它不会工作,因为它会尝试进入第一个回购而不是第二个。

如何创建公钥并告诉git我想推送到另一个仓库?

感谢。

1 个答案:

答案 0 :(得分:3)

您需要在~/.ssh/config文件中声明不同的ssh密钥,正如我在“How to manage multiple ssh keys in the ~/.ssh directory”中所解释的那样

我建议不要使用键的默认名称,而是:

~/.ssh/proj1
~/.ssh/proj1.pub
~/.ssh/proj2
~/.ssh/proj2.pub

然后有一个~/.ssh/config喜欢:

Host ghproj1
    User           git
    Hostname       github.com
    IdentityFile   ~/.ssh/proj1
    IdentitiesOnly yes
Host ghproj2
    User           git
    Hostname       github.com
    IdentityFile   ~/.ssh/proj2
    IdentitiesOnly yes

您需要更改两个回购中的原始网址:

cd /path/to/cloned/proj1
git remote set-url origin ghproj1:yourProject1

cd /path/to/cloned/proj2
git remote set-url origin ghproj1:yourProject2

详见:

ghproj1:yourProject1这样的网址是一个网站,它会明确使用您在~/.ssh/config中为ghproj1条目指定的密钥。

在OP的情况下(answer below),正确的网址是:

~/.ssh/id_recaprojekt

注意:您需要指定私钥的路径(私有,非公开,不是.pub

cd /path/to/cloned/plastickychirurg
git remote set-url origin plastickychirurg:michalfeher/plastickychirurg.git

cd /path/to/cloned/recaprojekt
git remote set-url origin recaprojekt:michalfeher/recaprojekt.git

请注意,我在主机条目中添加了“主机名”。

〜/ .ssh / config文件中对这些条目的所有想法都不是(重复不是)把gitgithub.com放在网址中(它是由与每个条目相关联的项目完成的任务):

所以:

 git@github.com:michalfeher/recaprojekt.git

与:

相同
 recaprojekt:michalfeher/recaprojekt.git

除了第二个网址将使用ssh密钥

相关问题