How To Easily Manage Multiple Git Configurations

Eugenio Carocci
Geek Culture
Published in
3 min readMar 5, 2023
Photo by Dan Cristian Pădureț on Unsplash

Some time ago, I had the necessity to work on my machine for different clients and to do so I needed to configure my Git client differently based on the project.

I looked around for common solutions out there, looking at pros and cons, and apparently the best approach is the one that I’m going to share with you all today.

You will thank me, I know it!

How does Git config works?

First, we need to understand how Git configuration actually works.

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places

  • [path]/etc/gitconfig file: Contains values applied to every user on the system and all their repositories. If you pass the option --system to git config, it reads and writes from this file specifically. Because this is a system configuration file, you would need administrative or superuser privilege to make changes to it.
  • ~/.gitconfig or ~/.config/git/config file: Values specific personally to you, the user. You can make Git read and write to this file specifically by passing the --global option, and this affects all of the repositories you work with on your system.
  • config file in the Git directory (that is, .git/config) of whatever repository you’re currently using: Specific to that single repository. You can force Git to read from and write to this file with the --local option, but that is in fact the default. Unsurprisingly, you need to be located somewhere in a Git repository for this option to work properly.

Each level overrides values in the previous level, so values in .git/config trump those in [path]/etc/gitconfig.

If you want to understand more about this look at this :)

And here’s the solution you’re looking for

For the purpose of explaining my solution I’m gonna suppose that I need to work for a couple of clients that will be called, with a lot of fantasy, client A and client B.

The idea is that all the projects belonging to client A will be in ~/clientA/ whereas those for client B will be in ~/clientB/.

Now you can define the following files

  • ~/.gitconfig-clientA
  • ~/.gitconfig-clientB

Each of those will hold client specific configuration such as the email, SSH configuration and all the other possible items.

Last piece of the solution is to update the already existing ~/.gitconfig file with the following

[includeIf "gitdir:~/clientA/"]
path = .gitconfig-clientA
[includeIf "gitdir:~/clientB/"]
path = .gitconfig-clientB

If you need to use different SSH keys based on the client folder you can simply add the following configuration to the correct ~/.gitconfig-clientA file.

[core]
sshCommand = ssh -i ~/.ssh/ssh_private_key_for_client

You can read more about how this sshCommand works by looking at the official documentation here.

So, what are you waiting for? Don’t be shy and give it a try! I hope you have enjoyed this article and see you all in the next one!

--

--