Sitemap
WAES

We are a Dutch software consultancy company, bringing the best software engineers from all over the world to the Netherlands 🇳🇱

Multiple git accounts on the same computer? — an easy guide

4 min readSep 22, 2023

--

As a developer, you may often find yourself in a situation where you’re using a single computer for both client projects and personal endeavors. While this flexibility offers advantages, it can lead to a perplexing problem: managing multiple git accounts on a single machine.

In this article, we’ll explore developers’ common challenges in this scenario and provide an effective solution to make life easier.

Press enter or click to view image in full size

The Problem

Maintaining distinct Git identities for personal and professional projects involves separate usernames, email addresses, and SSH keys. However, managing these identities can become annoying.

Just like when we forget to update our git configuration locally, and our new commit goes with the wrong author and email information to our personal repository (and you must use the superpowers of push -f, trying a desperate fix).

Having these identities manageable by folder would avoid this kind of embarrassing situation. The good thing is that it’s possible and easy, and I’ll show you how.

A little disclaimer: I will cover only the SSH way of pushing code to the repositories in this article. This strategy works for any OS with Git installed.

The Solution

From now on, I’ll assume you already have the company SSH key set up; thus, your ~/.ssh folder should contain at least two files, the public and the private key you already use as default for git. In my case, these keys are called id_ed25519 and id_ed25519.pub.

Creating our personal key

That said, let's create our personal key.

cd ~/.ssh
ssh-keygen -t rsa -C "personal@email.com" -f "github-personal"

ls
github-personal id_ed25519 known_hosts
github-personal.pub id_ed25519.pub known_hosts.old

Now your ~/.ssh folder should contain at least four files: github-personal, github-personal.pub, company private key, company public key (in my case, id_ed25519 and id_ed25519.pub, respectively)

Adding our personal key to a git provider

That said, the first thing you have to do now is to copy your new public key to your preferred cloud-based git website.

pbcopy < ~/.ssh/github-personal.pub

This command copies the public key to your clipboard. Now, it’s easy to put it in, for example, GitHub or GitLab.

Using our personal key in a good way

The good thing is since git v2.13, it’s possible to use conditional configuration includes. This is very helpful for a case like ours because it enables us to automatically apply a different gitconfig, depending on our working folder. This means we can use different usernames, emails, and ssh keys mapped by folder.

Get Fabrício Yamamoto’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s suppose we have our projects folder on our desktop. At the end of your ~/.gitconfig file, you can put something like this:

[includeIf "gitdir:~/Desktop/projects/personal/"]
path = .gitconfig-personal

If you use Windows, your config should be something like this:

[includeIf "gitdir:C:/Users/user/Desktop/personal/"]
path = .gitconfig-personal

Then you can create the ~/.gitconfig-personal file with something like this:

[user]
name = yourname
email = personal@email.com

[core]
sshCommand = ssh -i ~/.ssh/github-personal

And then the magic happens!

Not only our desired personal ssh, email, and name will be used, but every time you clone a repository inside ~/Desktop/projects/personal/ folder, it also means that our newly created github-personal ssh key will be used.

As long as you remember that your personal projects need to be cloned in ~/Desktop/projects/personal/, everything will be okay!

You can test the different configurations if you go inside a git repository in your personal folder, for instance, ~/Desktop/projects/personal/repository

$ cd ~/Desktop/projects/personal/repository
$ git config --show-origin --get user.email
file: ~/.gitconfig-personal personal@email.com

It’s also possible to add more and more configurations per folder, so if you have more than two different accounts, you just need to redo all the steps and choose another folder.

Conclusion

Using multiple git accounts on the same computer has been an effortless task since 2017. However, until then, many outdated solutions were spread on the internet, which are unfortunately still relevant for most search engines, like Google. Writing this article is my way of trying to make things better. I hope this solution was clear, straightforward, and fits your needs.

Do you think you have what it takes to be one of us?

At WAES, we are always looking for the best developers and data engineers to help Dutch companies succeed. If you are interested in becoming a part of our team and moving to The Netherlands, look at our open positions here.

WAES publication

Our content creators constantly create new articles about software development, lifestyle, and WAES. So make sure to follow us on Medium to learn more.

Also, make sure to follow us on our social media:
LinkedIn — Instagram — Twitter — YouTube

--

--