Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Multiple SSH keys for different github accounts

Multiple SSH Keys settings for different github account

create different public key

create different ssh key according the article Mac Set-Up Git

$ ssh-keygen -t rsa -C ""

Please refer to github ssh issues for common problems.

for example, 2 keys created at:


then, add these two keys as following

$ ssh-add ~/.ssh/id_rsa_activehacker
$ ssh-add ~/.ssh/id_rsa_jexchan

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then added

#activehacker account
	User git
	IdentityFile ~/.ssh/id_rsa_activehacker

#jexchan account
	User git
	IdentityFile ~/.ssh/id_rsa_jexchan

Clone you repo and modify your Git config

clone your repo git clone gfs_jexchan

cd gfs_jexchan and modify git config

$ git config "jexchan"
$ git config "" 

$ git config "activehacker"
$ git config "" 

or you can have global git config $ git config --global "jexchan" $ git config --global ""

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Another related article in Chinese


I had to change the [remote "origin"] / url field in my local .git/config to use the Host defined in .ssh/config in order for this to work, i.e.,

[remote "origin"]
        url =

Without that modification, git would just try to use my default ssh key.

sdeverly commented Nov 1, 2012

Had to do the same to get it working.

elkuku commented Feb 9, 2013

Me too πŸ˜‰

jsh2134 commented Apr 1, 2013

Me as well.

Instead of having to change the remote origin in the config after cloning, you can just use that custom Host variable on the git clone command instead.

jmpnot commented May 6, 2013


git clone gfs_jexchan

as oppose to:

git clone gfs_jexchan

that's the ticket!

malleor commented Jul 28, 2013


To make it work with Gists, I needed to add another entry in ~/.ssh/config:


If your ssh keys don't all show up with
ssh-add -l
you have to run
ssh-add ~/.ssh/yourkey.rsa

thanks, worked like a charm


ghost commented Aug 28, 2013

but how it can works with "rake deploy" when I deploy my octopress blog? The mapping ssh-key seems no effect....

nosir commented Sep 17, 2013

thanks, would be better if it could be changed as @jmpnot mentioned.

when trying to pull through a php script via exec it would not use the key automatically though it was the correct user configured (my user). Going through the ssh config file just saved the day! :D

ssh config +
updated clone url

Working great... :-) Thanks alot...

noonat commented Jan 28, 2014

I was having an issue with SSH using the wrong key, depending on which order I accessed repositories. Turns out it will try all the keys loaded into ssh-agent in order, and IdentityFile only adds to the end of that list.

You can add IdentitiesOnly yes the SSH config block to force it to only use the IdentityFile you specified.

avendael commented Feb 2, 2014

I was also having issues with SSH using the wrong key even after following the advice of most who have encountered the same issue. In my case, I have a globally enabled ControlMaster in my ssh config. Removing the global ControlMaster config and deleting the github ssh socket (which in my case, is stored in /tmp as ssh_hostname.com_user as specified by my ControlPath config) fixed the issue for me. I can still use ControlMaster in other hosts by adding a ControlMaster auto line in that host's block.


ghost commented Feb 16, 2014

@oleweidner: Thank you !

How do you do this with https urls though?

Thanks Jexchan. It helped and working as expected.

1Rhino commented Mar 27, 2014

Thank you. It worked fine.

RBeek commented May 26, 2014

What exactly is the rule for setting up "Host" in the config file
Is it always[accountname] or can it be anything, because I set it to the shortname of my repository. Everything works, but it is not quit clear what needs to be set there.

ignasne commented May 28, 2014

Tnx it helps a lot!

Thanks for the tips. Might help to add the following to the instructions:

git remote set-url origin git@<host-in-ssh-config>:<username>/<repo>

This is the same as editing your .git/config file.

Same question: How do you do this with https urls though?

gholadr commented Jun 23, 2014

Thanks for the help!!!

1ambda commented Jun 26, 2014


wmyers commented Jul 2, 2014

Thanks worked a treat. The following worked for me:
git clone


nikolasc commented Aug 4, 2014

I forked @RichardBronosky (by mistake :) ) and changed the Host and User settings in ~/.ssh/config (User ) which enables one to actually use multiple accounts, on the local machine, with SSH access.

I borrowed one of my friend's laptops, and created a github account. It took a while to figure out why I kept getting kicked off github 'as user <friend's username>' - he hadn't created a new user for me on the laptop, so I was accessing with his username set in ~/.gitconfig and used with SSH. So the above, along with my ssh config changes, was the best way to get around the issue without wrecking his setup.

I just found out another way no need to deal with multiple SSH keys: use gitcredentials's useHttpPath option

    helper = osxkeychain
    useHttpPath = true

English see
Chinese see

On Windows 7, I want to manage multiple github users with passphrase-protected ssh keys.

I sorted my ~.ssh/config file to use different keys for different repo domains as described above (I previously documented the steps on mint/ubuntu.)

However, I don't want to type my passphrase every time, and I don't want to use ssh-agent.

Here's how I set up Pageant to ask for and remember my passphrase on Windows login:

  • Download the Putty Tools package
  • Win+R to open run dialog
  • shell:startup to open the auto start folder in windows explorer
  • Navigate to your Pageant folder in a separate Explorer window
  • Right click and drag PAGEANT.EXE to the Startup folder. Choose Create Shortcut
  • Right click the shortcut, properties, Shortcut tab
  • Find the path to your .ppk encoded keypair
    • generate one with PUTTYGEN.EXE if you need a new one
    • convert one with PUTTYGEN.EXE if you have a linux id_rsa file but no .ppk
    • win7+ : shift right click your key in Explorer, Copy as path
  • Append the key path to the C:\whatever\PAGEANT.EXE line
    • e.g: "Target: C:\putty\PAGEANT.EXE plato_cambrian.ppk plato.ppk"
    • If your keys aren't adjacent to PAGEANT you can use absolute paths or change "Start in:" which is Pageant's CWD
  • Open the shortcut or reboot, and type your passphrases

Let's say alice is a user, with 2 or more private repositories repoN.
For this example we'll work with just two repositories named repo1 and repo2

You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers.
You want to perform git pull origin master for example, and you want this to happen without asking for a password.

You don't like dealing with ssh-agent, you have discovered (or you're discovering now) about ~/.ssh/config a file that let's your ssh client know what private key to use depending on Hostname and username, with a simple configuration entry that looks like this:

  User git
  IdentityFile /home/alice/.ssh/alice_github.id_rsa
  IdentitiesOnly yes

So you went ahead and created your (alice_github.id_rsa, keypair, you then also went to your repository's .git/config file and you modified the url of your remote origin to be something like this:

[remote "origin"]
        url = "ssh://"

And finally you went to the repository Settings > Deploy keys section and added the contents of

At this point you could do your git pull origin master without entering a password without issue.

but what about the second repository?

So your instinct will be to grab that key and add it to repo2's Deploy keys, but will error out and tell you that the key is already being used.

Now you go and generate another key (using ssh-keygen -t rsa -C "" without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:

  • repo1 keypair: (repo1.alice_github.id_rsa,
  • repo2 keypair: (repo2.alice_github.id_rsa,

You will now put the new public key on repo2's Deploy keys configuration at, but now you have an ssh problem to deal with.

How can ssh tell which key to use if the repositories are hosted on the same domain?

Your .ssh/config file points to and it doesn't know which key to use when it's time to do the pull.

So I found a trick with You can tell your ssh client that each repository lives in a different subdomain, in these cases, they will be and

So first thing is editing the .git/config files on your repo clones, so they look like this instead:

For repo1

[remote "origin"]
        url = "ssh://"

For repo2

[remote "origin"]
        url = "ssh://"

And then, on your .ssh/config file, now you will be able to enter a configuration for each subdomain :)

  User git
  IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
  IdentitiesOnly yes

  User git
  IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
  IdentitiesOnly yes

Now you are able to git pull origin master without entering any passwords from both repositories.

If you have multiple machines, you could copy the keys to each of the machines and reuse them, but I'd advise doing the leg work to generate 1 key per machine and repo. You will have a lot more keys to handle, but you will be less vulnerable if one gets compromised.

Let me add that it is not enough to have

  User git
  IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
  IdentitiesOnly yes

   User git
   IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
   IdentitiesOnly yes

In this case the repo1 block will point to the second block because the repo1 block says HostName which will redirect from first to second block.

Took me a while to figure out just how smart ssh is πŸ˜„

@burningTyger The HostNames can be identical - it is the 'Host' that must differ. What may work best is to use "", instead of that second "". Then clone with git clone

elpd commented Dec 24, 2014

Thank you. Worked as expected.

pajtai commented Jan 28, 2015

thank you @oleweidner for the tip on changing the git config!

dbolser commented Feb 19, 2015

Thanks dudels.

apiraino commented Mar 6, 2015

Thanks for the guide, helped me solve this very issue. I need a public keys for each of my GitHub repos.

go figure....

lazurey commented Jul 7, 2015

Thank you Xianjun!!!
Your answer is the first one when I searched this problem on google. πŸ˜„

Thank you very much for the instructions @jexchan ! Doing as @jmpnot wrote fixed all the problems !

Thank you so much for this guide, this worked wonders with my sublime text git plugin!

ttiu commented Nov 5, 2015

Works great. Thank you!

$ git config --local remote.origin.url git@[repository]-[public_ssh_key_file_name]:[repository_user]/[project].git
$ git config --local [user_email]

Works!!! ^^

The .git file looks like this

[remote "origin"]
  url = git@[repository]-[public_ssh_key_file_name]:[repository_user]/[project].git

I think others have mentioned this but I couldn't find it when I searched the page.

I was getting the following error on bitbucket:

$ git push -u origin master
conq: repository access denied.
fatal: Could not read from remote repository.

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

adding this to the ~/.ssh/config file entries after specifying IdentityFile fixed it ...

IdentitiesOnly yes

Contents of ~/.ssh/config:

User git
IdentityFile ~/.ssh/my_rsa_private_key
IdentitiesOnly yes

Contents of .git/config:

[remote "origin"]
>url =
>fetch = +refs/heads/:refs/remotes/origin/

Notice how matches in each.

AT LAST, I've seen someone else use subl command. I'm not alone πŸ˜„

wching commented Jan 19, 2016

@ashanbrown thanks for your line, saved me πŸ‘ πŸš€

[remote "origin"]
url =

Golden line, solved the problem.

vidinev commented Feb 16, 2016

On Ubuntu 15.10 I faced with β€œBad owner or permissions on ~/.ssh/config” error.
This article helped me

Just need:
chmod 600 ~/.ssh/config

What worked for me is to use "github-activehacker" instead of "".

Khande commented Aug 25, 2016

learned a lot, thx!

Worked pretty good, thanks!

Thanks, it worked pretty awesome!

eness commented Jan 14, 2017

Worked fine for me!

knatch commented Jan 19, 2017

Thanks @oleweidner !!

spasarok commented Jan 30, 2017

If you have to change your local .git/config for this to work, how are you supposed to make the first clone?

EDIT: I figured it out. Instead of changing the remote url in .git/config, you can clone by replacing with the host like so: git clone gfs_jexchan. This will automatically set the remote url in .git/config to use the ssh host, so no need to manually edit after cloning.

this is going to save my life!

Thank you for the documentation. It helped me. Could you please apply @oleweidner changes as well. It seems it is mandatory.

JREAM commented Feb 23, 2017

I find this really easy


git clone gh:jream/config-ubuntu.git


[url ""]
     insteadOf = "gh:"
     pushInsteadOf = "github:"
     pushInsteadOf = "git://"

[url "git://"]
     insteadOf = "github:"


      User git
      IdentityFile ~/.ssh/id_rsa

m4vr commented Feb 23, 2017


With a git push, if underlying ssh returns an error as "nodename or servname" unknown replace the ":" by a / in the origin url, like
(git config --get remote.origin.url) ssh://[reponame]/[username]/[reponame].git (at least with git 2.11.0)

inakiabt commented Jul 14, 2017

I finally made it work! πŸŽ‰
The issue in my case (I think it's related to was that I had this in my ~/.ssh/config (it's to make ssh connections persistent):

Host * !localhost
  ControlPath ~/tmp/ssh_mux_%h_%p_%r
  ControlMaster auto
  ControlPersist 2h

Whenever I connect to a server via SSH, that configuration checks if the files exists in ~/tmp if not, it creates a new one. But if it exists, it reuses the latest connection that created that file (I'm not pretty sure how exactly works, but it's something like that). With that ControlPath configuration, it creates files like: ~/tmp/ because %h is for "remote host", %p is for "port" and %r is for "ssh user". So, no matter which repository hostname I used, the "remote host" was always (because of the HostName configuration this gist suggest) and the connection was reused every time.

The trick was change my ~/.ssh/config from ControlPath ~/tmp/ssh_mux_%h_%p_%r to ControlPath ~/tmp/ssh_mux-%n-%p-%r.
%n is for "local hostname" (the Host config), so now this control path file is created like ~/tmp/

I was using the user as my github username rather than git . Hence having problems with connection . Now everything working perfectly . Thanks a lot .

I was facing problems but thank god this works properly. Thanks for sharing

cwangmove commented Sep 15, 2017

Thank you, JexChan! This post is really helpful.

According to my experiment, git config "myusername" and git config "" does NOT matter at all. What really matters is what oleweidner mentioned in the first comment of this post as following:

`I had to change the [remote "origin"] / url field in my local .git/config to use the Host defined in .ssh/config in order for this to work, i.e.,

[remote "origin"]
url =
Without that modification, git would just try to use my default ssh key.`

In addition, if your url from git hub is, when doing git clone, you need to do
$ git clone

navyad commented Sep 25, 2017

following worked for me:

  • update url for origin in .git/config as given for HOST in /.ssh/config
  • git config
  • git config

jagroop commented Nov 29, 2017

thanks mate πŸ‘ πŸ’―

Guys, hope you won't lose as much time as I did to realize that I just had to reopen the console.
If you have all set up correctly and all of the sudden it stops working, just close and open your console and try again.

So sad...

Another solution is to only have one active ssh key in the ssh-agent.

Example ssh config-file:
    ForwardAgent yes
    User jexchan

Only use one key in the ssh-agent at any time:
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa_jexchan

Change user:
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa_activehacker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment