Handling different Git configurations

Having different Git repositories, I used to configure user.email and user.name on a per repository basis to avoid commiting with the wrong identity. It was always a bit cubersome to set up when having a new repository.

I recently stumbled upon the Git includeIf config (available since Git 2.13) which allows loading an overriding configuration file depending on the repository location.

In practice, I now have a global personal ~/.gitconfig with:

[user]
    name = My name
    email = user.name@personal.example

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig.work

and in ~/.gitconfig.work:

[user]
    name = My name
    email = user.name@work.example

That way, all repositories located in ~/work automatically inherits that configuration and I do not have to setup per repository settings.

Some resources: