用于更多存储库的Git公钥



我有以下问题。

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

第二个项目,我创建了另一个公钥,当我想要推送到第二个存储库时,它一直要求第一个代码/.ssh/id_rsa的密码短语。但是id_rsa保存着我第一个项目的公钥。当然,当我输入passsphrase时,它不会工作,因为它会尝试推到第一个仓库,而不是第二个。

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

谢谢。

您需要在一个~/.ssh/config文件中声明不同的ssh密钥,正如我在"如何管理~/目录中的多个ssh密钥"中所解释的那样。ssh目录"

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

~/.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

你需要改变两个repos中的原始url:

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

查看更多:

  • "如何临时更改git ssh用户用于远程推送?"
  • "在终端上切换Github帐户有问题"

ghproj1:yourProject1这样的url是一个ssh url,它将显式地使用您在~/.ssh/config中为ghproj1条目指定的密钥。

在OP的情况下,正确的url应该是:

~/.ssh/id_recaprojekt

注意:需要指定私钥的路径(private,不是public,不是.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

请注意,我在Host条目中添加了"Hostname"。

~/中所有条目的概念。ssh/config文件不(重复)将gitgithub.com放在url中(它由与每个条目关联的项为您完成):

:

 git@github.com:michalfeher/recaprojekt.git

等于:

 recaprojekt:michalfeher/recaprojekt.git

除了第二个url将使用ssh密钥

最新更新