Trusted answers to developer questions

How to manage multiple Github accounts from a single machine

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Many of us have encountered a situation where you need to manage multiple Github accounts from a single computer – You may have two Github accounts, one related to work and the other for personal repositories.

To achieve this, all it takes is a very simple combination of ssh and git config. The below steps are meant for LINUX/UNIX users.

  1. Set up SSH keys
    Two different SSH keys need to be set up 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/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.

Create a second key for the personal Github account. Make sure that when you are prompted for the filename when generating the SSH keys for the second account, you give the appropriate filename.

$ssh-keygen -t rsa -b 4096 -C "personal.emailid"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): /home/user/.ssh/id_rsa_personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa_personal.
Your public key has been saved in /home/user/.ssh/id_rsa_personal.pub.
  1. 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” and enter your Github password to confirm.
  • Repeat this step for your other account with the appropriate keys.
  1. Create an SSH configuration file to manage the two separate keys.
  • The touch command will create the SSH config file if it doesn’t exist.
  • Add the content present in ssh-config-file.sh to the file on your system.
ssh-config-touch.sh
ssh-config-file.sh
# 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

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

  1. Setup your repo
    Replace the 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 when pushing to these repositories.

RELATED TAGS

general

CONTRIBUTOR

Anjana Shankar
Attributions:
  1. undefined by undefined
Did you find this helpful?