How can I push git with multiple accounts on one machine

强颜欢笑 提交于 2021-02-11 15:20:56

问题


I want to use two different github accounts to separate my school and my personal things. So I found the methods here,(https://youtu.be/fnSRBRiQIU8)

I successfully added two ssh keys on each account and this is my ~/.ssh/config file

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

I tried to push it but did not have luck.

In the youtube video and its written instruction describe,

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

I thought it is old way, so I did new way like this

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

Then I got this error message

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't line 1 and 3 equivalent? Is there other method that I can use two accounts on one machine? Thanks!


回答1:


The basic technique is to configure SSH with two new virtual (ie. fake) host names. They both point at github.com, but one uses one key and the other use the other. Your ssh config has a problem, it doesn't specify what the real host is.

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

That says "when you try to connect to github.com-SECONDARY, use the ssh key in ~/.ssh/id_rsa_secondary". But github.com-SECONDARY isn't real. You need to tell ssh that by adding a HostName line.

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

That's the first problem.

The second is you're not using that hostname in your remotes.

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

That's the hostname part. It should be github.com-SECONDARY like so.

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

Then ssh will know to use your special config for the github.com-SECONDARY virtual host.

There's a better info on this in the Q&A for "Multiple GitHub Accounts & SSH Config".



来源:https://stackoverflow.com/questions/37979374/how-can-i-push-git-with-multiple-accounts-on-one-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!