Table of content
- Introduction
- Set up SSH keys for each GitHub account
- Add the SSH keys to the SSH agent
- Add each public SSH key to the corresponding GitHub account
- Configure SSH for multiple accounts
- Clone repositories using specific SSH config
- 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
Office account
ssh-keygen -t rsa -b 4096 -C "office@gmail.com" -f ~/.ssh/id_rsa_office
Client account
ssh-keygen -t rsa -b 4096 -C "client@gmail.com" -f ~/.ssh/id_rsa_client
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)"
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
ssh-add ~/.ssh/id_rsa_office
ssh-add ~/.ssh/id_rsa_client
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
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)
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)
Copy the public key to your clipboard
cat id_rsa_personal.pub
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
Office GitHub Account
Host github.com-office
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_office
Client GitHub Account
Host github.com-client
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_client
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
Example
git clone git@github.com-personal:username/project.git
git clone git@github.com-office:user/git-repository-for-office.git
git clone git@github.com-client:user/git-repository-for-client.git
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"
git config user.email "personal@gmail.com"
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)