Git

Using Multiple Accounts on GitHub

If you’ve found this post, you very likely came across an error while trying to push code to a new second GitHub account.  The issue is, GitHub can’t differentiate between accounts in the same PC by default.  If you’d like to use two or more accounts in the same PC, the steps below are required.

SSH Setup

The first step is to generate a SSH key for the new account, as you will not be able to use the same SSH key for multiple accounts. GitHub’s document on the subject is actually quite good, so I won’t try to replicate. You may find it here .

Create SSH Config

#Delete cached keys
ssh-add -D

After creating your keys, use ssh-add to register them to be used by ssh:

ssh-add ~/.ssh/name_of_first_account_id_rsa
ssh-add ~/.ssh/name_of_second_account_id_rsa


#Create new file in your home's .ssh directory named config (no extension):
vi ~/.ssh/config

The file contents will look as follows:

Host github.com-first_account
        HostName github.com
        User git
        IdentityFile ~/.ssh/name_of_first_account_id_rsa

Host github.com-second_account
        HostName github.com
        User git
        IdentityFile ~/.ssh/name_of_second_account_id_rsa

What we are doing here is telling SSH which key file to use depending on the host. Notice that the HostName and User values are the same in both entries, but the Host is what we’ll use from here on when specifying a remote repo.

Project Git Config

We will now specify the GIT required user name and email for the specific project. If you have not already done so, either clone the requested project or run “git init” inside of the directory.

Once complete, apply the user properties inside of the project directory:

git config user.name "your_name"
git config user.email "your_email@your_email_domain.com"

Now this is the tricky part - When you set up the remote repository, don’t use the host as specified in the GitHub “Clone” dialog. Edit the entry by replacing the “@github.com” portion with your specific Host in the SSH config file.

For example:

git@github.com:your_repo/your_project.git

Becomes (assuming we want to use the second account):

git@github.com-second_account:your_repo/your_project.git

So to add the remote repo:

git remote add origin git@github.com-second_account:your_repo/your_project.git

Summary

I hope you found this tutorial useful. For me, the “tricky part” specified above is what took me a while to figure out. Enjoy your day.