05 April, 2021

Conditional Git configs

Since the begining of time, since I started using git that is. I've always had a global ~/.gitconfig file with some aliases, e-mail, name and whatnot.

Being a consultant working with different customers I always forget to change it when I switch client and on top of that many of my personal projects have niklas.nihlen@client-email in the commits.

Just about to change client now so I decided to look into it and found that git since version 2.13 supports something called Conditional Configuration Icludes. But since I have a hard time grocking git's official documentation I'll show a small example instead, for future me and whoever else is intrested :).

Scenario

I have a new client named client-x and I want all git commits in the client-x folder and subfolders to have the e-mail address given to me byt the client niklas@client-x, for everyting else I want to keep my personal e-mail niklas@personal. My folderstructure looks like this:

~/
├── .gitignore
├── dev/
   ├── personal-project-1
   ├── personal-project-2
   ├── personal-project-3
   └── client-x
        ├── client-repo-1
        └── client-repo-n

I still want all my aliases and stuff to be global. So I created a new gitconfig file in the client directory ~/dev/client-x/.gitconfig just containing my client provided e-mail address:

[user]
	email = niklas@client-x

Then I updated my global ~/.gitconfg to make use of the [includeOf]:

[user]
	name = Niklas Nihlén
	email = niklas@personal

[includeIf "gitdir:~/dev/client-x/"]
	path = ~/dev/client-x/.gitconfig

Works like a charm I don't need to remeber to switch e-mail addresses anymore!


Tags: