Pro Tips: Manage Multiple GitHub Accounts Efficiently Skip to main content

Search

30 Dec, 2020
1 min read

Manage multiple GitHub accounts like a pro!

Image
Manage multiple GitHub accounts like a pro! - Banner

Oftentimes it may happen that you have a Github account where you upload all your code for practice sessions or learning purposes. You have another Github account pertaining to your company profile. When you push your changes, be it to your own Github account or a project-specific, how do you make sure that the respective account is always used? Well, I am here to help!

Set up SSH Keys

Let’s assume your two Github accounts are named githubPersonal and githubWork, respectively.

Create two SSH keys, saving each to a separate file:

mgh 1 img

Save it as id_rsa_personal when prompted.

mgh 2 img

Save it as id_rsa_work when prompted.

The above commands set up the following files:

- id_rsa_personal

- id_rsa_personal.pub

- id_rsa_work

- id_rsa_work.pub

Add the keys to your Github accounts

Copy the key to your clipboard:

mgh 3 img

In case you do not have pbcopy installed. To install and use pbcopy:

mgh 4 img

Edit your bash file

mgh 5 img

Create alias

mgh 6 img

Refresh your bash

mgh 7 img

Add the key to your 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 the process for your githubWork account.

Create a configuration file to manage the separate keys

Create a config file in ~/.ssh/

mgh 8 img

Edit the file using the text editor of your choice, here I am using nano, which is readily available in Linux.

mgh 9 img

Paste the following in the config file:

mgh 10 img

Update stored identities

Clear currently stored identities:

mgh 11 img

It will show all identities removed

Add new keys:

mgh 12 img

Test to make sure new keys are stored:

mgh 13 img

Test to make sure Github recognizes the keys:

mgh 14 img

You should see something like this:

mgh 15 img

mgh 16 img

You should see something like this:

mgh 17 img

One active SSH key in the ssh-agent at a time

To keep things safe, we need to manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation. So that the active ssh key is used at the time of push

ssh-add -l will list all the SSH keys attached to the ssh-agent.

Clear currently stored identities:

ssh-add -D

Add the required ssh key

Change config name and email for a project

GitHub identifies the author of any commit from the email id attached with the commit description.

First, check the name the project is using:

mgh 18 img

Check the email the project is using:

mgh 19 img

If you need to change then do the following:

mgh 20 img