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!

@sergiocbueno
Copy link

Thank you!

@arisanglobal
Copy link

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

@Mtillmann
Copy link

Thanks a ton for this! I had weird issues where ssh -T git@github.com -vvv and ssh -T git@github-somethingelse -vvv both would say that they use the specified key but I got identified with the my default username on both. The solution was to create a new key and add it to the second account.

@danish9811
Copy link

will the purpose be the same for multiple bitbucket accounts, and what if we have two github and two bitbucket accounts

@zyend
Copy link

zyend commented Dec 22, 2022

Thank you!

@danish9811
Yes, I just add it to the config with the right settings.

@gurcankavakci-esen
Copy link

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?

Same issue.

@abymathewtranzmeo
Copy link

abymathewtranzmeo commented Jan 13, 2023

Thanks man, I think this is the best post about this topic.

@antonga23
Copy link

You're doing the lord's work king 👑

@AlanDeikman
Copy link

Saved my life.

@kstryjewski-1
Copy link

Works, thanks :)

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