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!

@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

@guidoprinc
Copy link

guidoprinc commented Jul 12, 2022

Thank for this solution and all the comments! This really helped!

@Snouzy
Copy link

Snouzy commented Jul 14, 2022

used many times on differents computers. Thanks again :)

@kgdin42
Copy link

kgdin42 commented Jul 15, 2022

Hey @oanhnn thanks for the amazing solution. I would like to add that when copying the remote repo url from github you have to edit the url to repalce github.com with github-username for the second account to work.

@anianJudy
Copy link

anianJudy commented Jul 19, 2022

Hey @oanhnn thanks for the amazing solution. I would like to add that when copying the remote repo url from github you have to edit the url to repalce github.com with github-username for the second account to work.

you are right!
specialy when you clone by http url before add git config. need change remote.origin.url to git url using github-superman.
example:
git@github-superman:<username>/<projectname>.git

@morganavr
Copy link

morganavr commented Jul 20, 2022

Thanks for this guide!

@andresmascl
Copy link

andresmascl commented Jul 29, 2022

This is brilliant. Thank you very much!

@ForceGT
Copy link

ForceGT commented Aug 10, 2022

https://github.com/tw-yshuang/Git_SSH-Account_Switch

Underrated stuff but all of this can be done with a command switch and no /.gitconfig stuff

@tw-yshuang
Copy link

tw-yshuang commented Aug 17, 2022

https://github.com/tw-yshuang/Git_SSH-Account_Switch

Underrated stuff but all of this can be done with a command switch and no /.gitconfig stuff

Thank you for the compliment, hope everyone can enjoy this tool, and click the star😁

@L-F-Escobar
Copy link

L-F-Escobar commented Sep 6, 2022

I had this working for a long time then suddenly today my configs stopped working.

I had to re-run ssh-add ~/.ssh/.... cmds again to properly switch between accounts. Any idea why I had to re-run ssh-add commands?

@rsbmkJ
Copy link

rsbmkJ commented Sep 13, 2022

Thanks so much, really you saved my life 👍

@khushab
Copy link

khushab commented Sep 19, 2022

Thanks 👍

@micochango
Copy link

micochango commented Sep 19, 2022

Life saver, thank you!

@volunteerMinHtet
Copy link

volunteerMinHtet commented Sep 28, 2022

Thank a lot

@dayachettri
Copy link

dayachettri commented Oct 3, 2022

Thanks a lot.

@1eliton
Copy link

1eliton commented Oct 7, 2022

it worked here <3

@jef
Copy link

jef commented Oct 12, 2022

HUGE! Thank you :)

@MarcosiOSdev
Copy link

MarcosiOSdev commented Oct 17, 2022

Thank you !
Nice.

@jdvivar
Copy link

jdvivar commented Oct 17, 2022

If you already have the repo set up, after the ssh config instructions, just do:

  • Remote is called probably origin
  • I have work and personal accounts, for this I used git@github-personal as host from your SSH config
  • user/repo-name.git for the Github repository

$ git remote set-url origin git@github-personal:user/repo-name.git

@sergiocbueno
Copy link

sergiocbueno commented Oct 30, 2022

Thank you!

@arisanglobal
Copy link

arisanglobal commented Nov 1, 2022

This is really helpful tips to work with multiple GitHub repositories. Thank you!

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