Managing two GitHub accounts on same machine using SSH

5 minute read

Recently my company made it mandatory to use a GitHub account made against the company email address. Before that, I was using my personal account. However, I still had my personal projects on my machine which I was syncing with my personal GitHub account. I did not want to switch machines for working on my personal projects and I did not want to keep logging in and out of accounts for doing it either.

Existence of Problem

I am sure many users have faced this issue and there are multiple users who have faced the same issue. A number of articles around the web and StackOverflow questions and answer are the evidence of the need. However after trying out a number of those, I came to the conclusion that either they are too complicated to setup (which means that if I have to set the same again on another computer, I would probably forget the steps, which is not okay) or they are cumbersome to use every single time I am cloning a new repo. One thing which I had tried and failed miserably at was creating a personal library for some utility (I had once written a Ruby-like Data Types wrapper for PHP) and then setting up a remote into my Office organisation so that I can work on the library on my free time and when it reaches a stable stage, I push the update to my office colleagues.

What’s solution?

VERY IMPORTANT: The solution is for macOS and Linux. If you are a Windows user, I don’t know how to solve this problem for you. I do not use Windows and don’t know how to fix this issue there. But I guess if you understand what is being done here, you can try applying it on Windows as well.

There are multiple solutions to the problem but the one which worked one was as following:

  1. Creating different SSH keys for personal and work accounts. Of course they need to saved with different names. I am using the key names as id_personal_rsa and id_work_rsa (you can choose whatever names you like for your key files). If you don’t know how to do that or what they are, this link on GitHub has all the articles that you would need to get started.
  2. Add both the keys (the public key, of course) to the respective GitHub accounts.
  3. Add both keys to the keychain (ssh-agent) using the ssh-add command. Note that you have to do this every time you boot up your computer.
  4. Configure the SSH config (~/.ssh/config) file to contain the key you want to use. My SSH config file looks like this:
    Host *
      UseKeychain yes
      AddKeysToAgent yes
      IdentityFile ~/.ssh/id_work_rsa
      ServerAliveInterval 15
    
  5. Write two scripts to switch the active key. If you know bash scripting well, you can club the two scripts into one and use arguments to switch keys.

The scripts are:

For switching to work (/Users/vaibhav/bin/gskw.sh):

#!/usr/bin/env bash
sed -i -e 's/id_personal_rsa/id_work_rsa/' ~/.ssh/config
git config --global user.email user@company.example.com
echo "Switched to work key"

For switching to personal key (/Users/vaibhav/bin/gskp.sh):

#!/usr/bin/env bash
sed -i -e 's/id_work_rsa/id_personal_rsa/' ~/.ssh/config
git config --global user.email personal.email@provider.com
echo "Switched to personal key"

The way it works is by replacing the key in the SSH config file when you run one of the scripts (depending on which one you run). Now, you can just run gskw.sh to use your work SSH key and run gskp.sh to use your personal SSH key. Easy!

Advantages:

  1. You have to do this just once.
  2. You don’t need to change the URLs of repos before cloning them.
  3. Works for all other SSH needs as well!
  4. I can set remotes to both my personal and my work GitHub accounts for a single repo. Just that I have to switch keys before pushing.

NOTES:

NOTE: Replace the emails to reflect your work and personal emails you use with the respective GitHub accounts.

NOTE: You would have to setup your Git name already before you can properly use the above scripts. Run git config --global user.email <YOUR NAME> to set that up.

NOTE: You should put the scripts in one of your directories in PATH to make sure you can access them easily. For example, the directory /Users/vaibhav/bin/ is added to my PATH.

TIP: Sometimes one of the keys would be there in the keychain already but in most cases, you have to add both of them separately by opening terminal and typing something like:

ssh-add ~/.ssh/id_personal_rsa
ssh-add ~/.ssh/id_work_rsa

One way to make things easy is to make sure that you have not put up a password on the SSH keys you create and add those two commands to either ~/.profile or ~/.bash_profile so they are automatically added when you login. Of course this makes the setup a bit insecure - if your machine falls in wrong hands, the malicious user could login to your GitHub account (and any other server/account where you use either of those accounts). So you have to make the choice properly.

If however, you are able to add the password for the keys into your system’s keychain so that they get unlocked automatically, then you should definitely put a password on the Keys and feed them into the keychain - that’s the most secure setup.

Other solutions

For those of you who want to try the other solutions, here is a list of links which might help:

  1. Multiple github accounts on the same computer?
  2. Github and Multiple Accounts
  3. Multiple GitHub Accounts & SSH Config
  4. Work with GitHub Multiple Accounts

Most of them just tell you to configure the SSH config where you change the URLs of the repositories you want to clone - difficult.

There is one more way to do this (just in case you were looking for more alternatives). The steps are:

  1. Create a directory on your disk somewhere outside of your $HOME directory. So if your username is vaibhav are on macOS, the directory should not be inside /Users/vaibhav and if you are on Linux then it should not be inside /home/vaibhav. We are going to assume you are going to create a common user directory and then create a directory git_repos inside that so that the full path is /Users/common/git_repos/.
  2. Change the permissions on the directory so that all users have full access to the directory (chmod -R 777 /Users/common/).
  3. Create another user on the system.
  4. Assuming your main account (which you use normally) is the work account, install (or create a new) personal SSH key there.
  5. You can use the login command to login into the personal account on the command line and then run the git commands accordingly.

Summary

There are multiple ways to using multiple GitHub commands but the best/easiest one is to create two keys, add them both to keychain and switch between them using scripts.

Updated:

Leave a comment