Managing Multiple GitHub Accounts on One Computer: A Step-by-Step Guide

Managing Multiple GitHub Accounts on One Computer: A Step-by-Step Guide

Introduction

Managing multiple GitHub accounts on a single computer can seem daunting, but with a structured approach, it becomes straightforward and efficient. Let’s suppose you have two GitHub accounts, https://github.com/rohit-office and https://github.com/rohit-personal, and you want to set up your machine to easily communicate with both.

By setting up unique SSH keys, adding them to the SSH agent, and configuring the ~/.ssh/config file, you can seamlessly switch between personal and professional accounts without manual intervention.

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

The setup can be done in 5 easy steps:

Step 1 - Create SSH keys for all accounts

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

$ cd ~/.ssh

Syntax for generating unique ssh key for an account is:

ssh-keygen -t ed25519 -C "your_email@example.com" -f "desired-key-name"

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 ed25519 -C "my_office_email@gmail.com" -f "github-rohit-office"
ssh-keygen -t ed25519 -C "my_personal_email@gmail.com" -f "github-rohit-personal"

Notice here rohit-office and rohit-personal are the username of my GitHub accounts corresponding to and email ids respectively.

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

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

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

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

Step 2 - Ensure the SSH Agent is running

Start it manually:

# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
> Agent pid 59566

Step 3 - Add SSH keys to SSH Agent

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

$ ssh-add -K ~/.ssh/github-rohit-office
$ ssh-add -K ~/.ssh/github-rohit-personal

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

Step 4 - Add SSH public key to the GitHub

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

For doing this we need to:

1. Copy the public key

We can view the public key either by viewing content of the github-rohit-office.pub file using:

# View file content without opening
$ cat ~/.ssh/github-rohit-office.pub
$ cat ~/.ssh/github-rohit-personal.pub

Then copying the content of it.

OR

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

# Copy file content to clipboard
$ cat ~/.ssh/github-rohit-office.pub > /dev/clipboard
$ cat ~/.ssh/github-rohit-personal.pub > /dev/clipboard

2. Paste the public key on GitHub

  • Sign in to GitHub Account

  • Go to Settings > SSH and GPG keys > New SSH Key

  • Paste your copied public key and give it a Title of your choice.

OR

  • Sign in to GitHub

  • Paste this link in your browser (github.com/settings/keys) or click here

  • Click on New SSH Key and paste your copied key.

Step 5 - Create a Config File and Make Host Entries

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

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

$ touch config

open config file in any editor of your choice or run the command below to open config in nano editor.

$ nano config

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

# Office account
Host github.com-rohit-office
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-rohit-office

# Personal account
Host github.com-rohit-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-rohit-personal

Step 6 - Cloning GitHub repositories using different accounts

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

# Syntax to clone using diffrent accounts
$ git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git

For Example: I am making a repository on my personal GitHub account and naming it TestRepo

Now for cloning the repo use the below command:

$ git clone git@github.com-rohit-personal:rohit-personal/TestRepo.git

Finally

From now on, to ensure that our commits and pushes from each repository on the system uses 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.

# Inside the git repo where office project located
$ git config --local user.email "my_office_email@gmail.com"
$ git config --local user.name "Rohit Tewari"

# Inside the git repo where personal project located
$ git config --local user.email "my-personal-email@gmail.com"
$ git config --local user.name "Rohit Tewari"

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 (Optional: if remote not already added).

# Syntax
$ git remote add origin git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git

# Inside the git repo where office project located
$ git remote add origin git@github.com-rohit-office:rohit-office/TestRepo.git

# Inside the git repo where personal project located
$ git remote add origin git@github.com-rohit-personal:rohit-personal/TestRepo.git

NOTE: git@github.com-{your-username} is necessary to clone using right GitHub account

Now you can use:

$ git pull
$ git push

Conclusion

Handling multiple GitHub accounts just got easier! By setting up SSH keys and configuring your machine, you can switch between personal and work accounts like a pro. No more mix-ups, no more hassle — just smooth sailing on your coding journey. Let’s keep building great things! 🚀✨