This post is about working with multiple Github repos from different Github accounts on the same local machine.

The easiest way to authenticate yourself when you pull from or push changes to Github is using SSH. To do so, you need to generate an SSH keypair on your local machine and add the public part of the keypair to your Github account as a new SSH key.

Once this is done, you will be able to pull and push to your repo(s) using SSH automagically and without being continually prompted to authenticate.

SSH keys for a Single Github Account

In order to do generate keys for access to a single Github account from your local machine, you can use many of the defaults when creating the keypair.

In the examples in this section, assume that me@mydomain.com is the email address that you used to create your Github account.

  1. Create an SSH keypair using ssh-keygen with the same label as your email login for Github
    • EITHER ssh-keygen -t rsa -b 4096 -C "me@mydomain.com"
    • OR ssh-keygen -t ed25519 -C "me@mydomain.com"
    • Save using the default filename unless you want to use a different name
    • Decide whether you want to enter a passphrase every time you make use of the key - I don’t
  2. Locate the keypair in ~/.ssh (usually id_rsa.pub or id_ed25519.pub depending on the algorithm used in your ssh-keygen command)
    • id_rsa <-- your private key
    • id_rsa.pub <-- your public key
  3. Display the contents of the public key
    • cat ~/.ssh/id_rsa.pub
  4. Add it as a new SSH key in Github for your account denoted by me@mydomain.com
    • Paste it into the Key field - the Title can be anything you like

Add SSH Key

  1. Now add a new Host entry for Github in ~/.ssh/config

This is an example for use with Github

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Notice that the IdentityFile refers to the private key of the keypair

Authenticating to your Github repo

When you want to clone a repo or add an upstream repo to your local project, you should use either of the following commands:

To clone a repo use this command

git clone git@github.com:YOUR_GITHUB_USER/YOUR_REPO.git

To add a new upstream repo to an existing (local) project use this command

git remote add origin git@github.com:YOUR_GITHUB_USER/YOUR_REPO.git

Provided you have set the ssh keys up correctly, added them to you ~/.ssh/config and the public key to your Github SSH keys, then either command should work without prompting for a username and password.

In your git@github.com:YOUR_GITHUB_USER/YOUR_REPO.git URL, the text github.com matches the Hostname entry in your ~/.ssh/config and ensures that the correct private key is used for authenticating against the Github account.

SSH keys for multiple Github Accounts

When you have more than one Github account that you want to access from your local machine, you will need to create an SSH keypair for each additional Github account.

In the examples in this section, assume that me-two@mydomain.com is the email address that you used to create your other Github account.

Assuming that you created your first key using the instructions above or similar from elsewhere, let’s look at what you need to do in order to generate the SSH keys and config for the second Github account.

  1. Create an SSH keypair using ssh-keygen with the same label as your second email login for Github

    • EITHER ssh-keygen -t rsa -b 4096 -C "me-two@mydomain.com"
    • OR ssh-keygen -t ed25519 -C "me-two@mydomain.com"
    • IMPORTANT Save the key using a different name than the default otherwise you will replace your first key! I’ll use id_rsa_account2 as the filename for this example
  2. You should now see the following files in your ~/.ssh/ directory

ls ~/.ssh

config
id_rsa
id_rsa.pub
id_rsa_account2
id_rsa_account2.pub
  1. Copy the contents of id_rsa_account2.pub and add it as a new SSH Key to the second Github account (in Github account settings)
  2. Now edit ~/.ssh/config to add the second Github account host entry

You should now have this in your ~/.ssh/config file

Host github.com
HostName github.com
User git    
IdentityFile ~/.ssh/id_rsa

Host github-account2
HostName github.com
User git    
IdentityFile ~/.ssh/id_rsa_account2

Authenticating to a Second Account

When you want to clone a repo or add an upstream repo to your local project (from the second Github account) then you should use either of the following commands:

To clone a repo use this command

git clone git@github-account2:YOUR_GITHUB_USER/YOUR_REPO.git

To add a new upstream repo to an existing (local) project use this command

git remote add origin git@ithub-account2:YOUR_GITHUB_USER/YOUR_REPO.git

IMPORTANT Notice how the github-account2 in the git URL refers to the second Host entry in your ~/.ssh/config file which will be used to retrieve the key to login to the second account.

You can repeat these steps to create keys for multiple other Github accounts if you need to. Just be aware that when you copy the Code remote URL from Github - it won’t reflect the correct Host from your ~/.ssh/config file, and so you should adjust it accordingly

Wrap Up

Hopefully this explains how you get your SSH keys set up correctly to work with more than one Github account from your local machine.