Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using multiple github accounts with ssh keys

Problem

I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.

  2. Edit/Create ssh config file (~/.ssh/config):

    # Default github account: oanhnn
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/oanhnn_private_key
       IdentitiesOnly yes
       
    # Other github account: superman
    Host github-superman
       HostName github.com
       IdentityFile ~/.ssh/superman_private_key
       IdentitiesOnly yes
    
  3. Add ssh private keys to your agent:

    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/superman_private_key
  4. Test your connection

    $ ssh -T git@github.com
    $ ssh -T git@github-superman

    With each command, you may see this kind of warning, type yes:

    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?

    If everything is OK, you will see these messages:

    Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access.
    Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
  5. Now all are set, just clone your repositories

    $ git clone git@github-superman:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "superman@org2.com"
    $ git config user.name  "Super Man"

Done! Goodluck!

@wisefool01
Copy link

wisefool01 commented Jan 11, 2022

Thanks so much. This was very helpful. A little thing you need to add in step 5 if the repo was not cloned:

git config core.sshCommand "ssh -i ~/.ssh/superman_private_key -F /dev/null"

This will let you use the superman_private_key file every time you fetch, pull, or push to the remote from project2 local repo.

Alternatively, if the repo was created locally (i.e. not cloned), setting the remote with the alias will also do that:

git remote add origin git@github-superman:org2/project2.git

@ashu12318
Copy link

ashu12318 commented Jan 18, 2022

Key is already in use

Getting above error while using the ssh key in multiple account.
This error is happening in step 1.2 as per your instructions.

Am i missing something?

@malJaj
Copy link

malJaj commented Jan 22, 2022

Thanks, this helped!

@leedrum
Copy link

leedrum commented Feb 3, 2022

Thanks, very helpful

@Debarshi95
Copy link

Debarshi95 commented Feb 10, 2022

Thanks, it helped.

@MishellSossa
Copy link

MishellSossa commented Feb 23, 2022

Thanks! great tip!

@marshallcode
Copy link

marshallcode commented Mar 1, 2022

Very helpful. Thank you

@saxsax1995
Copy link

saxsax1995 commented Mar 16, 2022

Doesn't work, i got git@github.com: Permission denied (publickey).

@SofijaErkin
Copy link

SofijaErkin commented Mar 16, 2022

@saxsax1995 .
The possible reason is that the key, that is, the RSA is not paired well. You need to regenerate the key pair.
Your problem reference this Stackoverflow.
Just take a look at my RoadMap(from terminal to Git-Account via SSH).
Multiple Git-Account Using witness Step 5.

@the-code-bae
Copy link

the-code-bae commented Mar 22, 2022

Great guide, it helps me a lot! And I can expand this guide with another trick.

In your ~/.gitconfig

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

After this setting, you don't have to change URL manually every time when you want to clone repos.

God bless you for this !!! @chiahsien

And thank you @oanhnn - I've used this guide numerous times!

@chapmandu
Copy link

chapmandu commented Mar 22, 2022

Great post.. Thank you!

@EdgarOrtegaRamirez
Copy link

EdgarOrtegaRamirez commented Mar 23, 2022

Make sure you don't have a global identity file set

Host *
-  IdentityFile ~/.ssh/key
+

@saingsab
Copy link

saingsab commented Mar 24, 2022

Wonderful
Saving my life....

@arifhshigri
Copy link

arifhshigri commented Mar 31, 2022

Thanks for the post

@webmatrixxxl
Copy link

webmatrixxxl commented Apr 3, 2022

You are a hero!

@karolisgrinkevicius-home24

You are true superman! 🦸

@felipeerazog
Copy link

felipeerazog commented Apr 22, 2022

I've followed the above instructions, but I can't push into the remote branch.

same here, any update?

@karolisgrinkevicius-home24

@felipeerazog I would try to use the following adjustment of Hosts in ~/.ssh/config.

# Default github account: oanhnn
Host github.com-your-username-for-default-account
   HostName github.com
   IdentityFile ~/.ssh/oanhnn_private_key
   IdentitiesOnly yes
   
# Other github account: superman
Host github.com-your-username-for-superman
   HostName github.com
   IdentityFile ~/.ssh/superman_private_key
   IdentitiesOnly yes

@felipeerazog
Copy link

felipeerazog commented Apr 25, 2022

Hey @karolisgrinkevicius-home24 thanks for your comment. I found that my error was in the clone command:

# Before:
git clone git@github.com:path/to/repo
# After:
git clone git@github-superman:path/to/repo

@NelzkieCoder
Copy link

NelzkieCoder commented Apr 27, 2022

Works Thanks

@ashwinbabus
Copy link

ashwinbabus commented May 4, 2022

When I try to test the connection (step 4) for the other GitHub account I got the following error,

ssh: Could not resolve hostname github-otheraccount: nodename nor servname provided, or not known

@felipeerazog
Copy link

felipeerazog commented May 9, 2022

Hey @ashwinbabus check your ~/.ssh/config file, make sure you have github-otheraccount listed as host

@dovdiienko-qlt
Copy link

dovdiienko-qlt commented May 12, 2022

Nice trick I also used for years. But as it turned out, that does not work with repos which have submodules. The .gitmodules contains URL to the linked GIT repo. In case if you use git@github-superman:... URL to the submodule repo, that might fail to work on another computer, as they do not have a superman alias

@chiahsien
Copy link

chiahsien commented May 12, 2022

Nice trick I also used for years. But as it turned out, that does not work with repos which have submodules. The .gitmodules contains URL to the linked GIT repo. In case if you use git@github-superman:... URL to the submodule repo, that might fail to work on another computer, as they do not have a superman alias

Try to add this setting to your ~/.gitconfig and see if it works.

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

@dovdiienko-qlt
Copy link

dovdiienko-qlt commented May 13, 2022

Try to add this setting to your ~/.gitconfig and see if it works.

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

After I've added that config, I cannot clone. Git hangs.

I've found another solution which works for me:

set GIT_SSH_COMMAND=ssh -i private_key_file -o IdentitiesOnly=yes
git clone user@host:repo.git

The source: StackOverflow.com

@lucsrods
Copy link

lucsrods commented May 24, 2022

Thanks mate!

@HuStmpHrrr
Copy link

HuStmpHrrr commented Jun 7, 2022

I suggest using Match Host for a must shorter config

@JuiceBro
Copy link

JuiceBro commented Jun 7, 2022

Come from Colt Steele udemy course.
Still thanks to you!

@svascot
Copy link

svascot commented Jul 1, 2022

Thank you! this is the best post ever about this topic!

@lautarogonzalez-santex
Copy link

lautarogonzalez-santex commented Jul 5, 2022

Thanks so much. This was very helpful. A little thing you need to add in step 5 if the repo was not cloned:

git config core.sshCommand "ssh -i ~/.ssh/superman_private_key -F /dev/null"

This will let you use the superman_private_key file every time you fetch, pull, or push to the remote from project2 local repo.

Alternatively, if the repo was created locally (i.e. not cloned), setting the remote with the alias will also do that:

git remote add origin git@github-superman:org2/project2.git

Thanks!! It was very useful

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