Skip to content

Instantly share code, notes, and snippets.

@bhatnagardivyanshu
Created April 7, 2020 18:39
Show Gist options
  • Save bhatnagardivyanshu/c0f2a300850e89fccca14ece15d17585 to your computer and use it in GitHub Desktop.
Save bhatnagardivyanshu/c0f2a300850e89fccca14ece15d17585 to your computer and use it in GitHub Desktop.
Using ssh keys for Multiple GitHub Accounts

We need two pairs of ssh keys to connect to two different Github accounts.

STEP 1

Check if you have the ssh keys created in the .ssh folder

$ cd ~/.ssh
$ ls -al

If you find a pair of files with name 'id_rsa' and 'id_rsa.pub' or similar, then you already have a pair of ssh keys which we can use.

If not, follow the steps below to create one

$ cd ~/.ssh
$ ssh-keygen -t rsa

Accept the defaults when asked by pressing enter and you'll have a pair of ssh keys with the names given above.

We'll use this pair for our personal account.

STEP 2

Now to create another pair, we'll use a different name. Use the command below to create the second pair.

$ cd ~/.ssh 
$ ssh-keygen -t rsa -f "id_rsa_work" -C "your_name@work_email.com"

Here, we create another pair but with different name, id_rsa_work, where we specify work in the name to make it clear it is going to be used for the work account.

Now, you must be having two pairs of ssh keys

id_rsa 
id_rsa.pub 
id_rsa_work
id_rsa_work.pub

STEP 3

Now, we need to add these two keys to our two different Github accounts. id_rsa.pub to the personal account and id_rsa_work.pub to our work account.

To do so, follow the below steps to add key to your Personal account:

  1. Go to github.com
  2. Click on your profile button in the top right corner, and choose Settings
  3. From the left menu, choose SSH and GPG keys
  4. Click on New/Add SSH key
  5. Give a title to the key, 'Personal Laptop', and paste the contents of the id_rsa.pub key. Mind it .PUB key.

Repeat these steps to add the key in your work account and make sure to copy the contents of the id_rsa_work.pub key.

STEP 4

Add the private keys from the pairs to your ssh-agent. Before adding, make sure your ssh-agent is running.

$ ssh-add ~/.ssh id_rsa
$ ssh-add ~/.ssh id_rsa_work

STEP 5

Next, we create a config file in the .ssh folder that will take care of using the correct pair of keys when we use either of the two accounts.

Create a config file.

$ cd ~/.ssh 
$ touch config

Open the config file with your favourite editor.

Paste the following code in the file

# Personal Account
Host github.com
        Hostname github.com
        User git
        IdentityFile ~/.ssh/id_rsa
# Work Account
Host github.com-work
        Hostname github.com
        User git
        IdentityFile ~/.ssh/id_rsa_work

Here, we're configuring to use the First block if the host we are accessing matches 'github.com' else use the second block.

NEXT STEPS...

To make the host different, all we have to do is, when cloning the repositories (using ssh) from our WORK account, replace the host of the repo with github.com-work, for example,

When you click the clone button on github and choose using SSH, github gives us a similar snippet

git@github.com:YourUsername/YourRepoName.git

All we need to do is replace the 'github.com' with 'github.com-work' when cloning the repositories from the WORK ACCOUNT.

This way, we change the host of the repository, and while accessing the repository through SSH, the configuration of the second block, we just defined in the config file, will be used.

For existing repositories

Also, you may be having repositories already cloned or have origin added. To make them work with your new ssh keys, we need to update the remote origin of the repositories.

Also, make sure you're using the SSH version of the clone since https won't make use of your ssh keys.

To update the origin for your WORK REPOSITORIES, do the following:

  1. Go to your remote repository
  2. Click on clone and choose 'using SSH'
  3. Copy the snippet
  4. Open your terminal and navigate to the directory of your project
  5. Run the command git remmote set-url origin ____
  6. Paste the snippet in the blank space and UPDATE THE HOST to ...@github.com-work...

Username being used for the commits

Now, one more issue you might need to solve is that of the username used in the commits you make. It is obvious to forget while cloning the repositories to update email which is local to your repository.

To solve this for future, I'd suggest to unset your global email using the below command

$ git config --global --unset user.email

Now, whenever you try to make a commit in your new repository, git will ask you to set your email. Now you can mindfully set the email according to the account the repository belongs to using the below command

$ git config user.email me@workemail.com

That's it. Now go on and create two different repositories in your Personal and Work account and follow the steps to verify you did it correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment