DEV Community

Jahid Hassan Shovon
Jahid Hassan Shovon

Posted on • Originally published at jahidhassan.hashnode.dev

How to Use Multiple GitHub Accounts on One Computer (Step-by-Step Guide)

Table of content

  1. Introduction
  2. Set up SSH keys for each GitHub account
  3. Add the SSH keys to the SSH agent
  4. Add each public SSH key to the corresponding GitHub account
  5. Configure SSH for multiple accounts
  6. Clone repositories using specific SSH config
  7. Configure Git user for each repository

Introduction

As developers, we often work across multiple projects, some for our organization, our clients project, and maybe a few personal ones too. Each of these may require a different GitHub account or GitLab or similar kind of tools, which can lead to authentication conflicts, commit misattributions, and deployment issues if not managed properly. To ensure a smooth workflow, it’s essential to configure your computer so that it can handle multiple GitHub accounts seamlessly. Today we learned how to manage multiple github accounts from one computer. If you know this process, you can ignore this post.

To make this easier to understand, let’s consider a simple scenario. I contribute to my company’s projects using my office account (office@gmail.com), handle client projects with my client account (client@gmail.com), and manage personal repositories through my personal account (personal@gmail.com).

Make a directory .ssh in c/Users/jahid directory (if no exist .ssh directory). Open gitbash terminal under .ssh directory.

Set up SSH keys for each github account

Every account needs to generate public and private ssh key.

Personal account

ssh-keygen -t rsa -b 4096 -C "personal@gmail.com" -f ~/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode

Office account

ssh-keygen -t rsa -b 4096 -C "office@gmail.com" -f ~/.ssh/id_rsa_office
Enter fullscreen mode Exit fullscreen mode

Client account

ssh-keygen -t rsa -b 4096 -C "client@gmail.com" -f ~/.ssh/id_rsa_client
Enter fullscreen mode Exit fullscreen mode

Note: After running this command, you need to enter a password and confirm the password. In future, when you push or clone any repository, you need to enter this password.

Add the SSH keys to the SSH agent

Start the SSH agent in the background =>

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

When you run this command in Git Bash, it will output something like: Agent pid 1234

Add your SSH private key to the agent:

ssh-add ~/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode
ssh-add ~/.ssh/id_rsa_office
Enter fullscreen mode Exit fullscreen mode
ssh-add ~/.ssh/id_rsa_client
Enter fullscreen mode Exit fullscreen mode

If the key is added successfully, you should see some file like =>
bash /c/Users/jahid/.ssh/id_rsa_personal

Verify the key is added =>

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

When you run this command in Git Bash, it will output something like:

4096 SHA256:xxxxxxxxxxxxxx /c/Users/jahid/.ssh/id_rsa_personal (your_email@example.com)
Enter fullscreen mode Exit fullscreen mode

Add each public SSH key to the corresponding GitHub account

Here I will say this process only for personal accounts. Follow this process for every github account.

Find your public SSH key

C:\Users\jahid\.ssh\id_rsa_personal.pub (on Windows)
Enter fullscreen mode Exit fullscreen mode

Copy the public key to your clipboard

cat id_rsa_personal.pub
Enter fullscreen mode Exit fullscreen mode

Note: This will display the contents of the public key. Copy everything shown in the output (starting with ssh-rsa and ending with your email).

Add a new SSH key in you responsible github account

Go to your GitHub account, navigate to Settings > SSH and GPG keys > New SSH key > Enter Title, Key type should be authentication key, Enter Key (Which you was copy form previous step) > Save

Note: Follow this process for every github account

Configure SSH for multiple accounts

Edit the sh c/Users/userName/.ssh/config

Personal GitHub Account

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode

Office GitHub Account

Host github.com-office
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_office
Enter fullscreen mode Exit fullscreen mode

Client GitHub Account

Host github.com-client
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_client
Enter fullscreen mode Exit fullscreen mode

Note: HostName should be which host used to store your repository. Also be careful about IdentityFile.

Clone repositories using specific SSH config

When you clone the repositories, make sure to use the corresponding Host as defined in your SSH config.

Format

git clone [Host]:[GithubUserName]/[repository].git
Enter fullscreen mode Exit fullscreen mode

Example

git clone git@github.com-personal:username/project.git
Enter fullscreen mode Exit fullscreen mode
git clone git@github.com-office:user/git-repository-for-office.git
Enter fullscreen mode Exit fullscreen mode
git clone git@github.com-client:user/git-repository-for-client.git
Enter fullscreen mode Exit fullscreen mode

Configure Git user for each repository

You need to configure for every cloned project

For example

You will configure your personal accounts clone project. Go to your project file, open a terminal and enter those command

git config user.name "personal"
Enter fullscreen mode Exit fullscreen mode
git config user.email "personal@gmail.com"
Enter fullscreen mode Exit fullscreen mode

N.B.: If anything isn’t clear or you have any suggestions, please feel free to share them in the comments — I’ll be glad to respond. If you notice any mistakes, I truly appreciate your feedback. Thank you for taking the time to read this post. Wishing you all happy and productive coding! 💻✨

Originally published on Hashnode.

Top comments (0)