Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Th3On3/f1151a85a1017c28c7a4e12748522606 to your computer and use it in GitHub Desktop.

Select an option

Save Th3On3/f1151a85a1017c28c7a4e12748522606 to your computer and use it in GitHub Desktop.
Using multiple github accounts with ssh keys

Problem

I have a GitHub account, it is oanhnn, but at office, I couldn't use my personal GitHub account. I have to use an other GitHub account, eg supermen. I want use both two accounts on same computer (use without typing password each times, when git push or pull).

Solution

Using ssh keys and ssh configure file to alias host and using multiple GitHub accounts

How to?

  1. Make key pair for each accounts and add it to GitHub accounts.

  2. Make ssh configure file (~/.ssh/config) like:

    # Default github account: oanhnn
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/oanhnn_private_key
       IdentitiesOnly yes
       
    # Other github account: supermen
    Host github-supermen
       HostName github.com
       IdentityFile ~/.ssh/supermen_private_key
       IdentitiesOnly yes
    
  3. Added ssh key to your agent by command:

    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/supermen_private_key
    
  4. Check that repo recognizes keys.

    $ ssh -T [email protected]
    $ ssh -T git@github-supermen
    
  5. Clone projects

    $ git clone git@github-supermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name  "Super Men"
    

Done! Have goodluck!

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