Mastering Multiple GitHub Accounts: A Step-by-Step Guide for Your Local Machine

Mastering Multiple GitHub Accounts: A Step-by-Step Guide for Your Local Machine

An e2e guide on setting up two or more GitHub accounts in your system

Recently I felt the need of setting up my work github account on my personal laptop. On a simple Google search I found few guides but none of them were smooth enough while following and also do not cover all my requirements.

So here I thought of sharing my git workflow for my personal and work related projects. Here on I’ll be illustrating setup for two accounts : personal and work.

Setup SSH key for both accounts

Context

There are multiple ways two connect your machine to your GitHub account hosted on GitHub server, such as GitHub CLI, SSH and deprecated http login.

If you have just one single account in your machine I’ll suggest you to use github CLI, it’s easy, fast and beginner friendly.

But for multiple accounts using SSH setup is the best way. So let’s start :

Create SSH keys

Let’s first generate SSH key for your personal account.

While running below command the terminal will ask for a paraphrase, you may leave it blank and hit enter. Also the email id and username need not to be exactly same as your actual credentials. The -C flag is a comment and -f flag is the file name where your key will be stored.

     $ cd ~/.ssh
     $ ssh-keygen -t rsa -C "keshav@gmail.com" -f "keshav_git"

Now repeat the same step for your work account.

  $ cd ~/.ssh
  $ ssh-keygen -t rsa -C "keshav_work@gmail.com" -f "keshav_work_git"

Add SSH keys to SSH agent

Your machine has a SSH agent to keep track of all SSH keys and use them while making SSH connections.

First you will need to start your ssh-agent :

eval `ssh-agent -s`
ssh-add

Now add ssh keys to your ssh agent.

ssh-add ~/.ssh/keshav_git
ssh-add ~/.ssh/keshav_work_git

Add SSH keys to your GitHub Accounts

Inside the ~/.ssh directory you will find two public keys file with the name as keshav_git.pub and keshav_work_git in my case. You can open these files in any of text editor of your choice such as Vim, nano, kwrite , notepad etc.

Let’s first open the keshav_git.pub file, it’s my ssh key for personal account. Copy the content of this file and paste it on your personal GitHub account as instructed below.

  • Goto Settings > SSH and GPG keys > New SSH Key

  • Paste your copied public key and give any easily identifiable name such as my_windows_machine etc.

Repeat the above steps for you work account as well.

Creating a config file

Create a file with the name config inside your ~/.ssh directory. Edit as per your naming convention and copy the below content inside config file.

     #keshav-personal account
     Host github.com-keshav-personal
          HostName github.com
          User git
          IdentityFile ~/.ssh/keshav_git

     #keshav-work account
     Host github.com-keshav-work
          HostName github.com
          User git
          IdentityFile ~/.ssh/keshav_work_git

Using new setup for using GitHub

With this setup you can clone and make changes in your personal and work related repositories. For example I want to clone my private repository which has the SSH url as

git@github.com:alpha951/keshav_private_repo.git

To clone this repo I need to run below command in my terminal. Note that I’ve modified to -keshav-personal this will ensure that my shell automatically fetch ssh key from ~/.ssh/config file with hostname as github.com-keshav-personal which is my personal GitHub configuration.

git clone git@github.com-keshav-personal:alpha951/keshav_private_repo.git

Make sure you push commits with correct author name

While working with multiple git accounts you don’t want to make a commit in your work repository with your personal git username. To avoid this just set the repository specific username in the cloned directory.

So for instance if I am working on my personal repository I’ll make sure to set below username and email in my cloned repository so it will override the globally setup username. GitHub identifies contributor’s GitHub profile using their emails so it’s the most that you set correct email in your repository.

git config user.email "keshav950personal@gmail.com" && git config user.name "alpha951"

Add correct remote origin to your cloned repository

To push or pull to the correct account we need to add the remote origin to the project. So for the example we discussed before I’ll use below command to setup correct remote origin.

 git remote add origin git@github.com-keshav-personal:alpha951

To update remote origin for existing repository you can use below command

git remote set-url origin git@github.com-keshav-personal:alpha951/keshav_private_repo

Now you can simply use git pull and git push commands to update your local projects and commit contributions to the remote.

Voila! You have successfully set up your GitHub accounts. I hope this helped.