How to Work With Multiple Github Accounts in One Machine

You’ve probably created your personal Github account for personal use or for your work in your machine and now you want to be able to use both accounts from the same computer. This post explains how to setup multiple Github accounts in a single machine.

Check for Existing SSH Keys

Check for available SSH keys by running:

$ ls -al ~/.ssh

This lists all existing SSH keys. If you see these two files:

id_rsa
id_rsa.pub

That means you have keys available and you can just use those for your personal use and create another pair for your work account.

Generate SSH Keys If You Don’t Have Any

If you don’t have any existing SSH keys, first you need to generate a pair for your personal use:

  1. Open a Terminal window and run:
    $ ssh-keygen -t rsa -C "your-email-address"
    
    This creates a new SSH key pair.
  2. Press Enter when prompted for the location to save the keys to save them in the default location.
  3. Type a secure passphrase and confirm.

Generate SSH Keys for Your Work Account

To generate another pair of SSH keys you have to make sure that you don’t overwrite your personal keys. Follow the next steps to do so:

  1. Open a Terminal window and run:
    $ ssh-keygen -t rsa -C "your-work-email-address" -f "id_rsa_companyname"
    
    This generates the SSH keys and saves the public key with your work email as tag and differentiate it from the other pair of keys.
  2. Press Enter when prompted for the location to save the keys to save them in the default location.
  3. Type a secure passphrase and confirm.
  4. Run $ ls -al ~/.ssh again to verify that you have two pair of keys created. You should have four files:
    id_rsa
    id_rsa.pub
    id_rsa_companyname
    id_rsa_companyname.pub
    

Add the Keys to the Corresponding Github Account

To avoid having to type in the username and password everytime you make a push to Github, you have to make Github trust the keys you have created. To do so,

  1. Use $ pbcopy < ~/.ssh/id_rsa.pub to copy the public SSH key. Use xclip -sel clip ~/.ssh/id_rsa.pub if you are using Linux.
  2. Log in to your personal Github account.
  3. Go to Settings.
  4. Select SSH and GPG keys under the Personal settings menu.
  5. Click the New SSH key button.
  6. Give it a title and paste the public SSH key in the text area below.
  7. Click Add key.

Repeat the steps above on your work Github account. Use pbcopy < ~/.ssh/id_rsa_companyname.pub.

Add the SSH Keys to the SSH Agent

Because you created keys with unique names, you have to register them with the ssh-agent to use them.

  1. Start the ssh-agent with:
    $ eval "$(ssh-agent -s)"
    
  2. Add the keys like so:
    ssh-add ~/.ssh/id_rsa
    ssh-add ~/.ssh/id_rsa_companyname
    

Create or Modify the Config File

You have to modify the ~/.ssh/config file to automatically load the keys into the ssh-agent.

1.If the config file exists in ~/.ssh modify it.
If it doesn’t exist, create it and make sure it contains the following values:

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

Host github.com-companyname    
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_companyname

2.Save the file.

This configuration makes the ssh-agent use id_rsa as key for any Git URL that uses @github.com and id_rsa_companyname whenever a Git URL uses @github.com-companyname.

Try It Out

You have now configured your SSH keys to work with different Github accounts. You can try it out by cloning or pushing to your different repositories. These are the different ways that you will have to keep in mind while doing so:

When cloning a repository for your personal account you will use:

$ git clone git@github.com:<personalaccountname>/<repositoryname>.git

When cloning a repository for your work account you will use:

$ git clone git@github-companyname:<workaccountname>/<repositoryname>.git

Note that when using your personal account you use git@github.com and when using the work account you use git@github-companyname.

That’s it, you’re all done!