Codementor Events

Managing multiple git profiles on one machine using ssh

Published Jul 26, 2023
Managing multiple git profiles on one machine using ssh

We all learned Git when we first started to get into tech. We learned to configure ssh locally and remotely so it becomes easier for us to contribute to Open source software or our own projects. We were used to forking, cloning, committing, and pushing the repository and everything went well.

In case you are not aware of setting up ssh key in your machine, Checkout this article

Now, Let's create a scenario to understand better where this article is going. You get your first job break into tech 🎉. Congratulations for staying up til now in the software community and contributing to it commit by commit.

You are told to work on your own primary system for the first few weeks. Now, You get to the remote repository and copy the clone repository link using ssh and paste it into your terminal
0b8406e8-b5fa-4b82-934e-5b865a4edbe4.png
You get the following error
dcf3c2df-b6bf-4ed3-9253-64c822286617.png

You see this error considering the fact that you have already setup ssh to one GitHub account and are now trying to clone another GitHub account's repository

The solution to this problem was long and confusing articles. To solve this problem temporarily, I use to work on 2 laptops. One for my personal contributions and one for my work-related contributions. But, there should be an effective way to do it right. So, Without wasting any time, Let's get into it.

Create a config file

touch config

While creating the ssh key in your .ssh/ folder, We will be using 2 different names public for a personal account and work for a professional/work account. Copy and paste the below text into your config file

#work account
Host github.com-work
   HostName github.com
   User git
   IdentityFile ~/.ssh/work
   IdentitiesOnly yes

#public account
Host github.com-public
   HostName github.com
   User git
   IdentityFile ~/.ssh/public
   IdentitiesOnly yes

Focus on the 2nd line after the comment (below #work account and #public account), That is:

#work account
Host github.com-work

#public account
Host github.com-public

When we clone the repository, The text between the @ and : is what Host is defined as. We can see from the below image to get a rough idea.
cba5827a-3949-496b-9a59-2ce421bd76bf.png
So, When we copy the clone ssh link, We need to make certain changes in the link in the terminal. Let us go through it.
2fc34f2f-35df-4f96-8560-d71d451d9a26.png

Change the text which lies between @ and : your clone link to match either the work-related account or the public-related account or any account. The text should be the same as the Host we made for each account.

That's it. That's how you can configure multiple GitHub accounts in your one single local machine and do awesome contributions.

Discover and read more posts from Shaik Mohammad Abdullah
get started
post commentsBe the first to share your opinion
Show more replies