如何在一台机器上推送多个帐户的git

时间:2016-06-22 22:38:23

标签: github

我想使用两个不同的github帐户来分隔我的学校和我的个人事物。所以我在这里找到了方法,(https://youtu.be/fnSRBRiQIU8

我在每个帐户上成功添加了两个ssh密钥,这是我的~/.ssh/config文件

# Default account
Host github.com
    User git
    IdentityFile ~/.ssh/id_rsa
# Second account
Host github.com-SECONDARY
    User git
    IdentityFile ~/.ssh/id_rsa_secondary

我试图推它但没有运气。

在youtube视频及其书面说明中,

1. git remote add origin git@github.com:SECONDARY/testing.git
2. git push -u origin master

我认为这是老路,所以我做了这样的新方式

3. git remote add origin https://github.com/SECONDARYusername/testing.git
4. git push -u origin master

然后我收到此错误消息

fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Aren的第1和第3行相当于?还有其他方法可以在一台机器上使用两个帐户吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

基本技术是使用两个新的虚拟(即假)主机名配置SSH。他们都指向github.com,但是一个使用一个密钥而另一个使用另一个密钥。你的ssh配置有问题,它没有指定真正的主机是什么。

# Second account
Host github.com-SECONDARY
    User git
    IdentityFile ~/.ssh/id_rsa_secondary

那说"当你尝试连接到github.com-SECONDARY时,请使用〜/ .ssh / id_rsa_secondary"中的ssh密钥。但github.com-SECONDARY并不真实。您需要通过添加HostName行告诉ssh。

# Second account
Host github.com-SECONDARY
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_secondary

这是第一个问题。

第二个是您未在遥控器中使用该主机名。

git remote add origin git@github.com:SECONDARY/testing.git
                          ^^^^^^^^^^

这是主机名部分。它应该是github.com-SECONDARY之类的。

git remote add origin git@github.com-SECONDARY:SECONDARY/testing.git

然后ssh会知道使用github.com-SECONDARY虚拟主机的特殊配置。

"Multiple GitHub Accounts & SSH Config"的Q& A中有更好的信息。