Configure multiple Github accounts on a single machine

Table of Contents

Sooner or later as a developer, you will find yourself in a situation where you’re working using multiple Github accounts. One is your company account, one is your personal account. In this post, I’m going to share a common approach that can help you achieve that.

We will be using ssh as the protocol to authenticate our git requests to Github server because we do not want to enter the password each time we do a git request using username/password authentication. To use ssh we will need a ssh key pair (private/public keys) to authenticate each git request. Your public key will be stored on Github server. It acts as a lock to guard your Github repositories. To access (unlock) the repositories you will need the private key stored on your local machine and acts as the key for that lock. For that reason, you always keep your private key secured.

Generate ssh key pair using ssh-keygen

On a Linux distribution machine like my Ubuntu machine, you can generate a ssh key pair by using the ssh-keygen command line. After you generated a key pair, you will find it under ~/.ssh directory.

Maybe in that .ssh directory, you can find some key pairs already there generated by someone else. The key pair normally will be id_rsa for the private key, id_rsa.pub for the public key. If you’re not sure about the key pairs generated before, for example the person who created the key pairs might have secured the key pairs with a passphrase while creating it, then create a new one.

In order to do it, run the following command on your terminal:

ssh-keygen -t rsa -C "bob@company.com" -f "id_rsa"

When you’re prompted to enter the passphrase, leave it empty.

This creates a new ssh key pair, using the provided email as a label and you will find in the ~/.ssh directory 2 new generated files: id_rsa and id_rsa.pub

Similarly, let’s generate another key pair for your personal Github account. In order to do it, run the following command on your terminal:

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

On my machine, this command generates id_rsa_personal and id_rsa_personal.pub files in the current directory where I invoke the command line.

Copy them to the ~/.ssh directory using the following command:

cp id_rsa_personal id_rsa_personal.pub ~/.ssh

Be careful because you do not want to overwrite the key pairs generated before in the ~/.ssh directory. To simplify things for this post, I assume that the pair (id_rsa, id_rsa_public) is absent in the ~/.ssh directory.

Copy the public key content to corresponding Github account

Let’s print out on the terminal the content of the public key generated above. Run the following commands:

cd ~/.ssh
cat id_rsa.pub

Then copy the output text and login to Github with your personal account. Then go to Settings > SSH and GPG keys > New SSH Key. Copy the output text and paste to the text area on Github. Name it, for example, “key generated using company A’s laptop”.

Make sure to name your SSH key something you can remember so that if you need to revoke a key later, you can easily tell which one you’re looking for.

Register generated key pairs with ssh-agent

The ssh-agent acts like a keychain that is responsible for holding the private keys. In addition to holding private keys it also handles signing the authentication data using private keys.

Before adding the new SSH key to the ssh-agent, ensure the ssh-agent is running by executing the following command:

eval "$(ssh-agent -s)"

Now when the ssh-agent is running, executing the following commands will add the new SSH key to the local ssh agent.

ssh-add -K ~/.ssh/id_rsa
ssh-add -K ~/.ssh/id_rsa_personal

Configure the ssh key pairs in ~/.ssh/config file

Now the fun part. Every time you invoke a git request from your terminal, your git client will look for the private key (it will ask the ssh-agent) and use it to authenticate itself to Github git server. Since you’re having multiple key pairs how the git client would know it?

It turns out, when the git client invokes a git request using ssh protocol, it can look at a config file at ~/.ssh directory named config. If it’s not there already, create one. Create a file named “config” with the following content:

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

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

Save it.

Now company becomes a custom host name for the Github URL corresponding to your company Github account. Later as you will see, you will be naming your remote git repository in conjunction with this host name. In the configuration file, we map this custom host name to the id_rsa key.

Configure the git user name and email information This is important because you’re having two emails: personal and company. Github identifies the author of any commit from the email attached with the commit. We need to tell git the proper information for the author. You can use the git commands like the following:

git config --global user.name "Bob"
git config --global user.email bob@company.com

to configure the proper information. The option –global can be substituted with –system or –local depending on your user case.

->In my case, the system-wide configuration is empty. I used the global configuration, so that if the local configuration is empty then the name and email are set to my name and my company email.

A little bit about git configuration

Git looks for the configuration information in 3 possible places:

First, the system-wide /etc/gitconfig file, which contains settings that are applied to every user on the system and all of their repositories. You can make Git read and write to this file by passing the –system option.

Second, the ~/.gitconfig (or ~/.config/git/config) file, which is specific to each user. You can make Git read and write to this file by passing the –global option.

Finally, the .git/config file of whatever repository you’re currently using. These values are specific to that single repository only. You can make Git read and write to this file by passing the –local option. (If you don’t specify which level you want to work with, this is the default.)

As a rule of thumb, git always looks at things from the narrowest place which is local, then home, then system-wide.

In my case, the /etc/config is already set with my company account name and email so I will configure the user.name and user.email using –local option.

The following command line will show all inherited values from: system, global and local

git config -l

User case 1: you want to clone your company project from Github

Let’s say your company Github repository URL is something like (you can get this URL from Github)

Turn it into something that your local machine can understand it

Companies usually setup multiple repositories under a company repository name or a team repository name. In the command above, company-repo means the company repository name.

Clone your company repository by running the following command:

git clone git@company:your-company-github-username/company-repo/remote-repo-name.git

It should then clone the repository to a directory named “remote-repo-name” inside your current directory.

User case 2: your company project (on your local machine) is not tracked by git yet and you want to push it upstream

Let’s say your company project is located in the “company-project” directory. On your terminal run the following commands:

cd company-project
git remote add origin git@company:your-company-github-username/company-repository.git
git push origin master

User case 3: your company project was cloned by using username/password authentication

Run the following commands on your terminal:

cd company-project
git remote set-URL origin git@company:your-company-github-username/remote-repo-name.git

Verify that the remote URL has changed:

git remote -v
> origin  git@company:your-company-github-username/remote-repo-name.git (fetch)
> origin  git@company:your-company-github-username/remote-repo-name.git (push)

Where “remote-repo-name” is the name of your project’s repository on Github.

User case 4: you want to clone your personal project from Github

Let’s say your personal Github repository URL is something like (you can get this URL from Github)

Turn it into something that your local machine can understand it

Clone your company repository by running the following command:

git clone git@github.com:your-personal-github-username/remote-repo-name.git

It should then clone the repository to a directory named “remote-repo-name” inside your current directory.

Go into this directory and configure the user.name and user.email information. On your terminal, run the following commands:

cd remote-repo-name
git config --local user.name "Bob"
git config --local user.email bob@gmail.com

Because I already set the global configuration to my name and my company email. This local configuration is desirable.

User case 5: your personal project (on your local machine) is not tracked by git yet and you want to push it upstream

Let’s say your personal project is located in a directory named “my-repository” and you want to push it to Github using your personal Github account.

Go into this directory and configure the user.name and user.email information. On your terminal, run the following commands:

cd my-repository
git config --local user.name "Bob"
git config --local user.email bob@gmail.com

Because I already set the global configuration to my name and my company email. This local configuration is desirable.

Initialize a git repository inside it:

cd my-repository
git init

Add necessary files to your staging area:

git add *

As an example, the command above adds all your project’s files to the staging area

Make your first commit to your local repository

git commit -m "first commit"

Now go to Github and create a new repository named “my-repository” using your personal Github account.

Now go back to your terminal and run the following commands:

git remote add origin git@github.com:your-personal-github-username/my-repository.git
git push origin master

That’s all. Happy coding!