Managing Multiple Github Accounts from a single machine

Recently, I ran into a situation where I needed to manage multiple Github accounts from a single computer. I have two github account, one related to work and the other for my personal repositories.
It is a very simple combination of ssh and git config. Before I start explaining the steps for the same, let me clarify that the following steps are meant for LINUX/UNIX users.

1. Set up SSH keys

You will need to set up two different ssh keys for the two accounts.

$ ssh-keygen -t rsa -b 4096 -C "work.emailid"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/****/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/anjana/.ssh/id_rsa.
Your public key has been saved in /home/anjana/.ssh/id_rsa.pub.
view raw github1.bash hosted with ❤ by GitHub
$ssh-keygen -t rsa -b 4096 -C "personal.emailid"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/****/.ssh/id_rsa): /home/****/.ssh/id_rsa_personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/anjana/.ssh/id_rsa_personal.
Your public key has been saved in /home/anjana/.ssh/id_rsa_personal.pub.
view raw github2.bash hosted with ❤ by GitHub

Make sure that when prompted for the filename when generating the ssh keys for the second account, you give the appropriate filename.

2. Add the ssh key to your github account

> On your github account, Go to your Account Settings
> Click “SSH Keys” then “Add SSH key”
> Paste your key into the “Key” field and add a relevant title
> Click “Add key” then enter your Github password to confirm
> Repeat this step for your other account with the appropriate keys.

3. Create a ssh configuration file to manage the two separate keys

$ touch ~/.ssh/config

Add the following in your ~/.ssh/config file.

# Default GitHub
Host personal-github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
# Work GitHub
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
view raw github3.bash hosted with ❤ by GitHub

Once you save this file, you need to configure your git repos on local accordingly.

4. Setup your repo

Replace “git@github.com” part in the remote origin of all your personal repositories to “git@personal-github.com”
And that’s it. From now on, the appropriate user account will be used for pushing to these repositories.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s