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
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.
There are several reasons why someone can have multiple Git accounts. Some common scenarios includes:
- A developer who has a personal account for their own projects, and a separate account for their work projects.
- A company that has a Git account for their internal projects, and a separate account for open-source projects.
- A team of developers who share a single Git account for collaboration on a project, but each member also has their own personal account.
- A developer who uses multiple Git hosting services (e.g. GitHub, GitLab, Bitbucket) and has a separate account for each service.
- A developer who uses multiple Git accounts for different client projects, to keep their work separate and organized.
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:
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
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 "email@example.com"
This command will generate a new SSH key pair (private & public) with the specified email as the key comment.
-t option specifies the type of key to generate (in this case, RSA),
-b option specifies the number of bits in the key (4096 bits is a recommended value).
Generally these keys have the naming convention as
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").
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 "firstname.lastname@example.org"
Add the public key to your Git hosting service account.
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.
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:
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 repository
$ 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.