How to Manage Multiple GitHub Accounts on the Same Machine

As developers we normally have to juggle around multiple GitHub accounts on the same machine. For example we have our own personal GitHub account for our own project and then another GitHub account that we use for our client project.

This article provides a step-by-step instructions on how to setup and work with multiple GitHub accounts on the same machine.

Manage Multiple GitHub Accounts

In this scenario we will create two different GitHub accounts on the same machine and then switch between the two.

Generate SSH Keys

First, we need to create our private/public SSH keys for our personal account.

We can do this by executing the following command in a terminal:

$ ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_personal"

The above email address is the one you use to login to your personal GitHub account.

When asked for the location to save the keys, accept the default location by pressing enter. A private/public key pair is created in the default ssh location ~/.ssh/.

Our personal SSH keys are:

~/.ssh/id_rsa_personal.pub and ~/.ssh/id_rsa_personal

Next, we create our private/public SSH keys for our client account:

$ ssh-keygen -t rsa -C "email@company.com" -f "id_rsa_company"

The above email address is the one you use to login to your client GitHub account.

The above command creates our client SSH keys locates in ~/.ssh/.

Our client SSH keys are:

~/.ssh/id_rsa_company.pub and ~/.ssh/id_rsa_company

Add SSH Keys to Respective GitHub Accounts

Login to your personal GitHub account and add your id_rsa_personal.pub personal public key.

Next, login to your client GitHub account and add you id_rsa_company.pub client public key.

If you’re not sure how to do this, then read install Git and Generate SSH Keys.

Update SSH config File

The SSH config file resides in ~/.ssh/. If you don’t see a config file, then create one:

$ cd ~/.ssh/
$ touch config           // Creates the file if not exists
$ nano config            // Opens the file for editing

Add your different GitHub profiles in the SSH config file:

# Personal account
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# Company account-1
Host github.com-company
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_company

Register SSH Keys with ssh-agent

Start your ssh-agent by running eval "$(ssh-agent -s)".

Then add your SSH keys to the ssh-agent:

ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_company

This will register your SSH keys with the ssh-agent on the machine.

Only One Active SSH Key in ssh-agent at a time

Now that we have created our SSH keys for personal and company and registered them with the ssh-agent, we can now easily switch between the two GitHub accounts on the same machine.

We need to make sure that we only have the respective SSH key added in the ssh-agent at a time.

For example, if we are working on our personal project we do:

$ ssh-add -D            //removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa_personal        // Adds the personal ssh key

Likewise, if we are working on our client project, we do:

$ ssh-add -D            //removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa_company          // Adds the company ssh key

And this is how we can manage multiple GitHub accounts on the same machine and switch between them while working on respective projects.