Git tricks: managing different configs

How to manage different Git configuration files.

Git tricks: managing different configs

As a consultant I often work for different customers. Some of them usually require you to have an account that ties into their central authentication and authorization system for logging into services.

One of these services is often a Git repository server such as GitLab, Bitbucket or GitLab Enterprise. You name it.

As part of compliance policies, you might also be required to commit changes using the email address tied into the account.

Personally, there are only 2 things I really care about when setting up my git configuration correctly:

  • Full name
  • Email

Some people also like to sign their commits, indisputably proving themselves as the author, but that's something that you way want to add yourself.


The goal I want to achieve with this blog post, is illustrating how you can have a global configuration for Git that applies to everything, and the ability to have overrides for every specific folder containing checkouts of different customer repositories.

This is my main global .gitconfig file, located inside my home directory.

08:57:51 carroarmato0@neon-flower ~ → cat ~/.gitconfig 
[user]
	name = Christophe Vanlancker
	email = carroarmato0@example.org
...
[includeIf "gitdir:~/Work/Customers/FatCatCorp/"]
	path = ~/Work/Customers/FatCatCorp/.gitconfig
[includeIf "gitdir:~/Work/Customers/GitRichAndFamous/"]
	path = ~/Work/Customers/GitRichAndFamous/.gitconfig

I've left out all the things that aren't relevant for this post, but the magic that makes it all possible are the includeIf options.

These will instruct Git, that if it finds a .gitconfig file present in the listed customer specific folders, it should load them.

Here is what the .gitconfig file looks like for those specific customers:

08:57:59 carroarmato0@neon-flower ~ → cat ~/Work/Customers/FatCatCorp/.gitconfig
[user]
	email = cvan@fatcatcorp.org

08:58:08 carroarmato0@neon-flower ~ → cat ~/Work/Customers/GitRichAndFamous/.gitconfig
[user]
	name = The Big Queso
	email = carroarmato0@gitrichtandfamous.fast

Not only can you override different aspects of your git config file per customer, but, it also doesn't matter where you are within the directory structure. Git will perform a reverse directory traversal until its able to find a .gitconfig file. You can even still override a specific repository by just configuring it locally.

Mastodon