git multiple ssh account

Manage Multiple Git Account using SSH, in One Device

Git is a very powerful tool for managing your source code. There are a lot of advantages of using Git as Version Control System (VCS). But sometimes some things can be really tiring. Like managing multiple git accounts using the SSH of course. That is when you have one or more accounts or one or more git service. Let us see how we can manage multiple Git account. It is not really hard at all, I understand it looks intimidating at first. Without further ado let us see how we can manage multiple Git accounts.

Why would you need two or more accounts ?

If you are reading this casually without facing the problem. Here is a quick scenario on why you would need to use multiple git account in your device.

The scenario: You have 2 email accounts and have accounts on Gitlab and GitHub too. And your source code for multiple projects are on these two emails and git service.

The problem: You cannot use the same SSH Key for multiple user account on the same provider ( i.e: Gitlab or GitHub ). But you have to push / pull code from multiple user’s as per the project setup.

How will you solve this problem ? Yes, exactly following this guide.

Also Read: Learn Interesting Uses of ‘ls’ command in Linux


Managing two or more git account road map

Here I will outline how we are going to approach the problem step by step and later we will see the actual steps.

  • Create new key pair for each account ( as per email )
  • Create config file and set it up
  • Add the keys to ssh agent to enable automatic key switching
  • Testing if the process if it worked

Here I have listed the approach. Now let us see how to actually do all those steps below.

Creating SSH keys

Change into the ~/.ssh directory before moving on.

To create new SSH key pair we are going to use the command ssh-keygen .

Enter the file name for the key as shown in the image below. The name should identify the key uniquely as we will be using it later. Also you do not need to put in any passphrase just press enter.

Now that we have successfully create new keys do that for personal too.

Also Read: NVM Install and Manage Multiple NodeJS version


Adding keys to your account

Add the contents of ~/.ssh/ to your git service provider account.

If you don’t know how to to this look for SSH Keys section in the settings page of your account. It’s really not that hard.

GitHub documentation & Gitlab documentation.

Creating the SSH config file

Next step is to make sure we create a new configuration file for git. The location for this file is ~/.ssh/config.

# this is for personal
# host is the name / identifier we will be using
# you can change personal to anything you seem suitable
# this will be used later
Host personal
   HostName  # change this to your need or
   User git # this will be same
   IdentityFile ~/.ssh/id_rsa_nwao # path to the key pair we have created earlier for work

# this is for work
Host work
   User git
   IdentityFile ~/.ssh/id_rsa

# add as much host as you likeCode language: PHP (php)

The above content is what should be inside the config file. Add it for me there are only two

Also Read: Steganography: Hide text within an Image file using steghide


Adding SSH keys to agent

Before we add new identity file we need to remove all previous identities. Remove all identities by doing: ssh-add -D.

Now to add new keys you we have to do ssh-add <name_of_file>. In our case we need to do ssh-add ~/.ssh/id_rsa_gitlab_office.

That is all we need to do, if you edit the config file later you need to add the SSH keys again.

Testing our SSH config

Let us test the SSH configuration that we have set up by running the command below

ssh -T personal # personal is Host from the config fileCode language: Bash (bash)

Here is the output for each of my two hosts.

The output from each of the above. We have the username which we have setup using SSH key.

Also Read: 5 Productivity Tips and Tricks for Ubuntu


Change the origin URL

Try the git push or pull command it might not work for you now. That is because we added multiple identities.

To be able to do git actions you must change the existing origin URL following the below convention.

git remote set-url origin git@<host-in-ssh-config>:<username>/<repo>Code language: HTML, XML (xml)

So in our case (the example) it will be as under:

git remote set-url origin git@work:<username>/my-awesome-repo.gitCode language: HTML, XML (xml)

git clone should be modified too

Also note that you have to use the same structure while cloning from now on. SSH will look in the config for work to get all the related information.

git clone git@work:<username>/my-awesome-project.gitCode language: HTML, XML (xml)

That is all we need to know to manage multiple git account.

We have learned how to manage multiple git account on one device. If you have any confusion comment below.

Related Posts