Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaymondSHANG/0ff65d5f102a1e62761d76a83e69d9ea to your computer and use it in GitHub Desktop.
Save RaymondSHANG/0ff65d5f102a1e62761d76a83e69d9ea 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
       
    # Other github account: supermen
    Host github-supermen
       HostName github.com
       IdentityFile ~/.ssh/supermen_private_key
    
  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  "Supper Men"
    

Done! Have goodluck!

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