How to manage multiple Git accounts on one computer using SSH config

How to manage multiple Git accounts on one computer using SSH config

In this article we will learn about how we can effortlessly switch between Git accounts on a single computer.

This article is originally written in my blog. Do show some love there as well : How to manage multiple Git accounts on one computer using SSH config

Problem Statement

If you have multiple Git accounts and you need to use them on a single computer, you may encounter issues with authenticating to the different accounts.

Why will i have multiple Git accounts?

There are several reasons why someone can have multiple Git accounts. Some common scenarios includes:

  1. A developer who has a personal account for their own projects, and a separate account for their work projects.
  2. A company that has a Git account for their internal projects, and a separate account for open-source projects.
  3. A team of developers who share a single Git account for collaboration on a project, but each member also has their own personal account.
  4. A developer who uses multiple Git hosting services (e.g. GitHub, GitLab, Bitbucket) and has a separate account for each service.
  5. A developer who uses multiple Git accounts for different client projects, to keep their work separate and organized.

Ideal Solution

This article focuses on the SSH method as, HTTPS is very simple and uses traditional Username & password(token) for authentication.

Basically this is a two step solution:

Let's see these steps in detail:

Generate SSH Key pairs

SSH keys are a secure and convenient way to authenticate with Git hosting services such as GitHub, GitLab, and Bitbucket.

To set up SSH keys for a Git account, you first need to generate a new SSH key pair. This can be done using the ssh-keygen command. For example, to generate a new SSH key pair for your first account, you can use the following command:

$ ssh-keygen -t rsa -b 4096 -C "personalemail@email.com"

This command will generate a new SSH key pair (privat & public) with the specified email as the key comment. The -t option specifies the type of key to generate (in this case, RSA), and the -b option specifies the number of bits in the key (4096 bits is a recommended value).

Generally these keys have the naming convention as id_rsa and id_rsa.pub. You need to add the public key to your Git hosting service account to be able to use the ssh key to manage the repository.

For example, if you are using GitHub, you can go to your GitHub profile settings, click on the "SSH and GPG keys" tab, and then click on the "New SSH key" button. Paste the contents of the public key file (usually ~/.ssh/id_rsa.pub) into the "Key" field, and give the key a descriptive name (e.g. "Personal laptop").

git-ssh.png

Similarly, You can generate a separate SSH key pair for the second account by following command. You can use id_rsa_work as prefix for the work keys.

$ ssh-keygen -t rsa -b 4096 -C "workemail@email.com"

Add the public key to your Git hosting service account.

Use SSH config file to manage multiple Git accounts

The SSH config file is a hidden file (usually ~/.ssh/config) that allows you to define aliases for your Git hosting service accounts. These aliases can be used in place of the full SSH URL when cloning or pushing to a repository, which simplifies the process and makes it easier to switch between accounts.

If you don't have this file you can simply create one.

touch ~/.ssh/config

Add the following content in your SSH config file to define aliases for each of these accounts:

Host personal
  HostName github.com
  User gitUsername
  IdentityFile ~/.ssh/id_rsa

Host work
  HostName github.com
  User gitUsername
  IdentityFile ~/.ssh/id_rsa_work

This configuration defines two aliases: personal and work. The HostName and User options specify the server and user for the Git hosting service, and the IdentityFile option specifies the path to the SSH key file for the account.

Finally, Once you have defined these aliases in your SSH config file, you can use them to manage repositories using SSH URLs.

For example, to clone a repository using your personal account, you can use the following command:

$ git clone personal:username/repository.git

This command will clone the repository using the personal alias and the corresponding SSH key file (~/.ssh/id_rsa). Similarly, to clone a repository using your work account, you can use the following command:

$ git clone work:username/repository.git

The below command can be used to change the remote url of existing git respository

$ git remote set-url origin personal:username/repository.git

The other commands can be simply used as you normally do.

$ git pull origin branchName // pull latest from  branchName
$ git push origin branchName //push to branchName

This way, you can use multiple Git accounts on a single computer without authentication errors, and you can easily switch between them when cloning repositories.

Thank you for reading the guide on managing multiple Git accounts on a single computer. I hope that you found the information and examples in this guide helpful and useful. If you have any questions or feedback, please don't hesitate to comment.

Happy Coding!