Rui Fernandes

Working with multiple Git accounts in the same computer with SSH

by Rui Fernandes -

Imagem de cover

Introduction

Let's suppose that you have a Git account (Github, Gitlab, Bitbucket, etc.) where you put personal projects and a work account, which you likely need if you work for a company. In that case, you'll need to switch between them, depending on the project you are working on. How to do that easily? I'll show you from scratch.

Configuration

First of all, we'll set up SSH because it is a secure way to connect to a Git platform, and it doesn't require a username and a password every time I push or pull.

Personal Account

Open your terminal (Linux/macOS) or Git Bash (Windows).

Generate a SSH key:

ssh-keygen -t rsa -C somemail@ruimail.com.br
ssh-keygen

It will ask for a name to the file: SSH asking for the key file name

If you want to set a different name, remember to type the entire path to the file, like:

But if you want the default name, type enter.

You can also set a password, if you want. Else, let it empty. SSH asking for a password

Confirm password: SSH asking for the password confirmation

And then, check and copy your public key (in this example, I called id_rsa.pub):

cd ~/.ssh
cat id_rsa.pub

Now, go to your Git SSH configuration and add that public key (you must use it for one account in one git platform):

Work Account

Generate a SSH key:

ssh-keygen -t rsa -C someworkmail@company.com.br
ssh-keygen

You can repeat the same process, but you must set a different name for that key, like work_rsa. Remember to type the entire path to the file, like:

And then, check it out and copy it (in this example, I called work_rsa.pub):

cd ~/.ssh
cat work_rsa.pub

Log in on another account of your Git platform and repeat the same process of the personal account:

Using both accounts locally

Create a config file

Linux/macOS

To use both accounts, you must create a config file for ssh. For that, use your favorite text editor (like vim, nano, gedit, etc.):

cd ~/.ssh

## Using VIM
vim config

## Using Nano
nano config

Windows

On Windows, you'll need to open Git Bash and create a config file, running this command:

cd ~/.ssh

notepad config

Configuring hosts

Now, you'll need to configure the hosts:

## Personal Account (Github)
Host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa

## Work Account (Github)
Host github.com-work
 HostName github.com
 IdentityFile ~/.ssh/work_rsa

Notice that the word "work" (on the second host, after github.com-) could be any word provided it's different from the other hosts. In that case, for repositories of your work account, you'll clone from git@github.com-work. For your personal account, git@github.com.

Save and close.

Now, you can test it by cloning a private repository with SSH. On Windows, open Git CMD. On Linux/macOS, open your terminal.

## Personal Account
git clone git@github.com:account1/privaterepo.git

## Work Account
git clone git@github.com-work:account2/privaterepo.git

Bonus: switching the account associated to the commits quickly

So, we configured the authentication part. But you still can't easily switch the account associated with the commit messages. We'll do that now!

Before that, ensure you have a git config file:

cat ~/.gitconfig

If you don't have it, run a global config command to set up a username and an e-mail to your commit messages.

git config --global user.name "yourusername"
git config --global user.email "someemail@ruimail.com.br"

Manually, it's a bit tedious because you need to type that commands every time you want to change. But you can also create scripts for that. For instance, I use three scripts: personalgit, workgit, and checkgit (without extension).

#!/bin/bash

USER_NAME="yourusername"
USER_EMAIL="someemail@ruimail.com.br"

git config --global user.name $USER_NAME
git config --global user.email $USER_EMAIL

echo $USER_NAME
echo $USER_EMAIL
#!/bin/bash

USER_NAME="yourworkusername"
USER_EMAIL="someworkemail@company.com.br"

git config --global user.name $USER_NAME
git config --global user.email $USER_EMAIL

echo $USER_NAME
echo $USER_EMAIL
#!/bin/bash

git config --global --list

On Linux/macOS:

## Give permission to execute
sudo chmod +x ./personalgit
sudo chmod +x ./workgit
sudo chmod +x ./checkgit

## Move to the binaries folder
sudo mv personalgit /bin
sudo mv workgit /bin
sudo mv checkgit /bin

Now you can use these commands on your terminal: Commands working on Linux

On Windows (with Git Bash):

Note: "username" is a placeholder here.

cd /c/Users/username
mkdir .mygitbinaries

mv personalgit .mygitbinaries
mv workgit .mygitbinaries
mv checkgit .mygitbinaries

To run those scripts on any folder, you need to add them to the PATH variable. Open ~/.bashrc:

notepad ~/.bashrc

Now, export PATH (you can write this at the end of the file):

export PATH=/c/Users/username/.gitbinaires:$PATH

In my case:

Bashrc file with PATH exporting

Restart your Git Bash, and you'll be able to use these commands:

Commands working on Windows

Conclusion

Congrats! Now, you are able to use multiple Git accounts on your machine in an automated and easy way.

Do you have any questions?

Thanks for reading!

Attributions

Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

Rui Fernandes

Rui Fernandes

Focused Full Stack Engineer creating impactful digital solutions.