Skip to content

Instantly share code, notes, and snippets.

Star You must be signed in to star a gist
What would you like to do?
Using multiple github accounts with ssh keys


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).


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
       IdentityFile ~/.ssh/oanhnn_private_key
       IdentitiesOnly yes
    # Other github account: superman
    Host github-superman
       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
    $ ssh -T git@github-superman

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

    The authenticity of host ' (' 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 ""
    $ git config  "Super Man"

Done! Goodluck!

Copy link

Excellent post .

Copy link

chenxiao218 commented Mar 12, 2023

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

This works to me, thanks.

Copy link

Pfuufy commented Apr 7, 2023

Very helpful. Thanks!

Copy link

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

Hi, thanks for your good tips 👍, its worked for my case by your great comment, after following the steps above, we also have to reset the remote origin url with our custom "git-acount-host" in the config


Thanks man

Copy link

Thank you man! You are a life saver!


Thank you! Note that if you have ssh multiplexing set up globally, especially with persistence, you may want to disable it for github, since multiplexing works per-machine and not per Host entry. Add ControlMaster no to each Host configuration.

Copy link

This is golden, thanks!

Copy link

aicals commented Jun 2, 2023

Pure gold! Thanks!

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