Multiple Github Accounts on Single Machine

Intro

When we're working with multiple github accounts, there's high chance that you're going to need at least two or more github accounts and you need to switch between them easily. This is another easy way to have multiple git accounts on a single machine without having to specify which github host you're going to use. For example, you need two profiles. 1 for personal, 1 for work.

Steps

Setting Up Accounts and Set to Github

First thing first, make sure you already have both github account set up. Then, create a ssh key for both account.

Let say your personal github account is under john.doe@adhy.dev and your work account is under john.doe.workhard@adhy.dev.

You can use ssh-keygen -t rsa -C "john.doe@adhy.dev" to generate the public/private rsa key pair. When asked which file to save the key, you can use id_rsa_personal, id_rsa_my_work, or any names you prefer, but make sure you're not overwriting each account.

After the new key pair created, go to your github profile > Settings > SSH and GPG Keys. Create new ssh key there and paste the value of the newly created RSA key.

If you've done it for both account, then you're ready to go to the next step!

Setup ssh config

Now, some other tutorials might suggest that you need multiple profile setup on the ssh config file. Some references: Tutsplus Freecodecamp

but since I'm too lazy to specify which account to use when I'm working on something, I do this differently.

What I do is creating a ssh config for each profile as txt files containing the template below:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/REPLACE_WITH_YOUR_ID_RSA_FILENAME

Create those files for each of your accounts. So for our example, we can make it as ~/.ssh/config.work.txt and ~/.ssh/config.personal.txt. Replace the IdentityFile to match each of your rsa key pair file.

If you did it, your ~/.ssh folder should have at least these files:

config.personal.txt
config.work.txt
id_rsa_personal
id_rsa_personal.pub
id_rsa_work
id_rsa_work.pub

the names above should be different, depending on the filenames you specified and saved earlier.

Automating it Using a Script

Now let's create a gitswitcher shell script containing script as follows. You can adjust the script same as what you want.

echo "GIT SWITCHER 🔀 \nPlease select which git profile you want."
read -p "(p) for personal and (w) for work: " selection

if [[ $selection == "w" ]] ; then
  echo "WORK Profile selected. happy working!"
  cp -f ~/.ssh/config.work.txt ~/.ssh/config
  git config --global user.name "John Doe Work"
  git config --global user.email "john.doe.workhard@adhy.dev"
else
  echo "PERSONAL Profile selected. have fun self developing!"
  cp -f ~/.ssh/config.personal.txt ~/.ssh/config
  git config --global user.name "John Doe Personal"
  git config --global user.email "john.doe@adhy.dev"
fi

Foila, you're ready to go! You can simply run this shell script to switch between git account seamlessly! Don't forget to set the right permissions (using chmod) to let this script be executable and also you can symlink it as a global binary to use. The step to do this might different depending on your OS, but for me on MacOS, what I did is simply use ln -s [path to shell script] [path to symlink].