How To Work With Multiple Github Accounts on a Single Machine

RAHUL PANDEY
4 min readNov 1, 2022
Work with multiple GitHub accounts on a single machine

Let's suppose I have two GitHub accounts, https://github.com/rahul-office and https://github.com/rahul-personal. Now I want to set up my mac to easily talk to both the GitHub accounts.

NOTE: This logic can be extended to more than two accounts also. :)

The setup can be done in 5 easy steps:

Steps:

  • Step 1: Create SSH keys for all accounts
  • Step 2: Add SSH keys to SSH Agent
  • Step 3: Add SSH public key to the GitHub
  • Step 4: Create a Config File and Make Host Entries
  • Step 5: Cloning GitHub repositories using different accounts

Step 1:

Create SSH keys for all accounts

First, make sure your current directory is your .ssh folder.

$ cd ~/.ssh

The syntax for generating a unique ssh key for an account is:

ssh-keygen -t rsa -C "your-email-address" -f "github-username"

here,

-C stands for comment to help identify your ssh key

-f stands for the file name where your ssh key get saved

Now generating SSH keys for my two accounts

ssh-keygen -t rsa -C "my_office_email@gmail.com" -f "github-rahul-office"ssh-keygen -t rsa -C "my_personal_email@gmail.com" -f "github-rahul-personal"

Notice here that rahul-office and rahul-work are the usernames of my GitHub accounts corresponding to my_office_email@gmail.com and my_personal_email@gmail.com email ids respectively.

After entering the command the terminal will ask for a passphrase, leave it empty and proceed.

Now after adding keys , in your .ssh folder, a public key and a private will get generated.

The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case github-rahul-office and github-rahul-personal)

Step 2:

Add SSH keys to SSH Agent

Now we have the keys but they cannot be used until we add them to the SSH Agent.

    ssh-add -K ~/.ssh/github-rahul-office
ssh-add -K ~/.ssh/github-rahul-personal

You can read more about adding keys to SSH Agent here.

Step 3

Add SSH public key to the GitHub

For the next step, we need to add our public key (that we generated in our previous step) and add it to the corresponding GitHub accounts.

For doing this we need to:

  1. Copy the public key

We can copy the public key either by opening the github-rahul-office.pub file in vim and then copying the content of it.

    vim ~/.ssh/github-rahul-office.pub
vim ~/.ssh/github-rahul-personal.pub

OR

We can directly copy the content of the public key file into the clipboard.

     pbcopy < ~/.ssh/github-rahul-office.pub
pbcopy < ~/.ssh/github-rahul-personal.pub

2. Paste the public key on Github

  • Sign in to GitHub Account
  • Goto Settings > SSH and GPG keys > New SSH Key
  • Paste your copied public key and give it a Title of your choice.

OR

Step 4:

Create a Config File and Make Host Entries

The ~/.ssh/config file allows us to specify many config options for SSH.

If the config file does not already exist then create one (make sure you are in the ~/.ssh directory)

    touch config

The commands below open config in your default editor….Likely TextEdit, VS Code.

    open config

Now we need to add these lines to the file, each block corresponding to each account we created earlier.

     #rahul-office account
Host github.com-rahul-office
HostName github.com
User git
IdentityFile ~/.ssh/github-rahul-office
#rahul-personal account
Host github.com-rahul-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-rahul-personal

Step 5:

Cloning GitHub repositories using different accounts

So we are done with our setups and now it's time to see them in action. We will clone a repository using one of the accounts we have added.

Make a new project folder where you want to clone your repository and go to that directory from your terminal.

For Example., I am making a repository on my personal GitHub account and naming it TestRepo Now for cloning the repo using the below command:

git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git

[e.g.] git clone git@github.com-rahul-personal:rahul-personal/TestRepo.git

Finally

From now on, to ensure that our commits and pushes from each repository on the system use the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.

To do this use the following commands.

git config user.email "my_office_email@gmail.com"
git config user.name "Rahul Pandey"

git config user.email "my-personal-email@gmail.com"
git config user.name "Rahul Pandey"

Pick the correct pair for your repository accordingly.

To push or pull to the correct account we need to add the remote origin to the project

git remote add origin git@github.com-rahul-personal:rahul-personal

git remote add origin git@github.com-rahul-office:rahul-office

Now you can use:

git push

git pull

Thank you for sticking till the end, I hope this post was instructive as well as valuable for your time.

Please leave claps if this post helped to achieve what you were looking for and stay tuned for more such helpful posts.

--

--

RAHUL PANDEY

Engineer, Developer. Love to design scalable systems. Love to code and collaborate.