/assets/git-logo.svg

How to create a directory based .gitconfig

Motivation

Sometimes, if you work on different projects or with different companies, it’s important to have different git configurations across your projects.

Usually in this case you need to configure it on a repository basis like

sh
git config user.name "Myrmod"
git config user.email "myrmod@myrmod.de"

Even though this works totally fine, it can get quite tedious. Especially with a lot of different projects like you would have when working with an agency.

Solution

So there is another way to reduce your workload, so you have to configure it once per .gitconfig, not per project. Conditional includes are helping us here. Using them your ~/.gitconfig could look something like the following:

sh
[user]
name = Myrmod
email = myrmod@myrmod.de
# project/customer specific
[includeIf "gitdir:~/Projects/my-agency/"]
path = ~/Projects/my-agency/.gitconfig
[includeIf "gitdir:~/Projects/my-agency/customer/"]
path = ~/Projects/my-agency/customer/.gitconfig

Afterwards you only have to create whatever .gitconfig you like, at the specific location. You can check if you correctly configured it, by creating a commit and checking you log by using the git log command.