Introduction
Git is a great tool for version control. It is used by many developers to manage their code repositories. But what if you have multiple Git accounts? For example, you have a personal account and a work account. You want to use both accounts on the same computer. How can you do that? In this article, we will discuss how to manage multiple git accounts on one computer.
Create new SSH keys
First, We need to create SSH keys for each account we want to manage.
ssh-keygen -t rsa -b 4096 -C "your_email_address"
The terminal will ask you to enter a file in which to save the key. You can name it something like id_rsa_personal
or id_rsa_work
.
This file will be stored in the .ssh/
folder in your home directory.
Add the SSH key to the ssh-agent
Next, we need to add the SSH key to the ssh-agent.
ssh-add ~/.ssh/id_rsa_personal
Note: Do this for each SSH key you want to manage.
Add the SSH key to your GitHub account
Now, we need to add the SSH key to our GitHub account. To do this, we need to copy the contents of the public key files. You can do this by running the following command.
cat ~/.ssh/id_rsa_personal.pub
Copy the output of this command and add it to your GitHub account. You can do this by going to your GitHub account settings, clicking on the SSH and GPG keys
tab, and then clicking on the New SSH key
button.
Paste the contents of the public key file in the Key
field and give it a title.
Configure Git to use the appropriate private key
Now, we need to configure Git to use the appropriate private keys for each account.
Create a config file
To do this, we need to create Git configuration files for each account.
touch ~/.ssh/config
Add SSH configuration
Next, we need to add the following configuration to the config
file.
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
Note: Replace
github.com
with the hostname of the Git hosting service you're configuring.
Configure Git to use the appropriate account
Now, we need to configure Git to use the appropriate account for each repository. To do this, we need to add the following configuration to the .gitconfig
file.
Consider this to be the global Git configuration file. This has to be done to ensure that Git uses the correct account for each repository.
Create Git configuration files for each account
Here we plan to use the ~/.gitconfig
file as the global Git configuration file. If you already have a global Git configuration file, you can add the following to it.
[user]
name = personal_username
email = personal_email
Note: Replace
personal_username
andpersonal_email
with your GitHubusername
and
Since we use ~/.gitconfig
as the global configuration, this means that all repositories will use the personal account by default. To use the work account, we need to define a location for work repositories.
Let's say we use a folder called work/
in our home
directory for these repositories. Inside this folder, we will create a .gitconfig
file. This will be the local Git configuration file for repositories in the work folder.
Add the following configuration to the ~/work/.gitconfig
file.
[user]
name = work_username
email = work_email
Note: Replace
work_username
andwork_email
with the appropriate GitHubusername
and
Update the global Git configuration file
Now, update the global Git configuration file to use the ~/work/.gitconfig
file for repositories in the ~/work
folder.
Your updated ~/.gitconfig
file should look like this.
[user]
name = personal_username
email = personal_email
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
This means that Git will use the ~/.gitconfig
file for all repositories except those inside the ~/work
folder. For those repositories, it will use the ~/work/.gitconfig
file.
So make sure that you have the correct configuration in both files.
Ping GitHub to verify the SSH key
Now, we need to ping GitHub to verify the SSH keys. To do this, we need to run the following command.
ssh -T github.com
ssh -T github.com-work
If you get the following message, it means that the SSH key is working.
Hi personal_username! Youve successfully authenticated, but GitHub does not provide shell access.
Now you can clone repositories from both accounts using the following commands.
Note: Use the appropriate SSH URL for your repository and make sure that you use the correct host.
git clone git@github.com:personal_username/repo_name.git
git clone git@github.com-work:work_username/repo_name.git
That's it. You have successfully configured Git to manage multiple accounts on one computer. Now you can use different Git accounts for different repositories.
Table of Contents
- Introduction
- Create new SSH keys
- Add the SSH key to the ssh-agent
- Add the SSH key to your GitHub account
- Configure Git to use the appropriate private key
- Create a config file
- Add SSH configuration
- Configure Git to use the appropriate account
- Create Git configuration files for each account
- Update the global Git configuration file
- Ping GitHub to verify the SSH key