Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pavelfomin/33cdec67f68b591bde5b730c7b5dda47 to your computer and use it in GitHub Desktop.
Save pavelfomin/33cdec67f68b591bde5b730c7b5dda47 to your computer and use it in GitHub Desktop.
Using multiple github accounts with ssh keys

From https://gist.github.com/oanhnn/80a89405ab9023894df7

Problem

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

Solution

Use ssh keys and define host aliases in ssh config file (an alias per 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
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/id_rsa
       IdentitiesOnly yes
       
    # Other github account: superman
    Host github-personal
       HostName github.com
       IdentityFile ~/.ssh/personal_id_rsa
       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-personal
  5. Now all are set, just clone your repositories using github alias github-personal

    $ git clone git@github-personal:pavelfomin/spring-boot-rest-example.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment