33

GitHub Users,

I am newbie on github and have some issue with git setup. I have 2 account with different users on github and i have setup git on my system 2 times

First /.ssh folder (as usual) (contain id_rsa which copied in first account)
Second /.ssh/new folder (contain id_rsa which copied in second account)

now at the time of push how can i switch between ssh key?

Because if i would like to push for second account it will use .ssh key instead of .ssh/new and gives me error.

Please make me correct if i am understood something wrong here.

Thanks.

Beau
  • 3
  • 2
Arun Rana
  • 8,426
  • 14
  • 67
  • 107
  • 1
    I wonder why would you want to place "second" keys in `~/.ssh` sub-folder? Why not to stick to convention of having key suffixed by numbers, like: `id_rsa`, `id_rsa.1`, `id_rsa.2`, etc... – Juicy Scripter Jan 19 '12 at 10:18
  • @JuicyScripter Just want to make separation , you are also right however i need to tell git which key need to use and how? – Arun Rana Jan 19 '12 at 10:27

1 Answers1

50

(I've voted to close this as a possible duplicate, but I might as well add a similar answer anyway.)

When using the SSH transport, GitHub identifies you as a user based on the SSH key that you use to authenticate. So, you need to make sure that git is using one SSH key for one repository and another for the other.

I'm going to assume that:

  1. You have a GitHub account called user1, and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/id_rsa. Let's say that the repository you're interested in accessing as user1 is user1/whatever on GitHub.
  2. You have a second GitHub account called user2 and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/new/id_rsa. Let's say that the repository you're interested in accessing as user2 is user2/whatever on GitHub.

The simplest way to do deal with this is to create a new "remote" (i.e. a nickname for a remote repository) for each repository, where the hostname is in each remote's URL is actually an alias that you've set up in ~/.ssh/config. (If that config file doesn't exist, you'll have to create it.)

For example, one entry in the ~/.ssh/config file might look like:

Host github-as-user1
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/id_rsa

Then you can add a remote called gh-user1, say, with:

git remote add gh-user1 git@github-as-user1:user1/whatever.git

... and then if you want to push your master branch to the repository user1/whatever on GitHub using the ~/.ssh/id_rsa key, you can just do:

git push gh-user1 master

In order to push as the other user (user2) to the second repository, you need to add a second alias to you ~/.ssh/config file. For example:

Host gh-as-user2
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/new/id_rsa

Then to push to that second repository as the different user, you can just do:

git push gh-user2 master
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 2
    So git will not work with out .ssh/config? Why can't git take a parameter like standard ssh? -i .ssh/my_github_key – Bradley Kreider Jun 13 '12 at 22:20
  • 2
    I would add that this can be tested through `ssh -T git@gh-as-user1` and `ssh -T git@gh-as-user1`. If it does not work, try to run `ssh-add ~/.ssh/new/id_rsa` or `ssh-add ~/.ssh/id_rsa` – Benoît Legat Apr 23 '13 at 16:10
  • 4
    If the keys you're using exist outside of the ~/.ssh directory, they won't work unless you do: `ssh-add /path/to/key` – Dean Rather Jun 16 '15 at 02:45