0

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!

jayko03
  • 2,329
  • 7
  • 28
  • 51
  • No, `https://github.com/SECONDARYusername/testing.git` isn't the same as `git@github.com:SECONDARY/testing.git`. – Adrian Jun 22 '16 at 22:45
  • @Adrian Is it because one is for ssh and another one is for https? – jayko03 Jun 22 '16 at 22:46
  • 1
    Check http://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config and see if that answers your question. – Schwern Jun 22 '16 at 23:03

1 Answers1

2

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".

Community
  • 1
  • 1
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    Thank you. I fixed as you advised and it works totally fine. I read the link you provided before, but didn't understand completely. – jayko03 Jun 23 '16 at 00:52