Conditional .gitconfig

Publication date: 2023-09-10

I have multiple sets of credentials: different names, different mail addresses, different commit signing keys, and I work with several sets of Git repositories: for example, for work I use one mail and key, but for my personal open-source work, I use another one.

How to manage that? The common way would be to just set git config <your_name>, git config <your_email>, git config user.signingkey <your_key> for every repository you want to work with. But this is tedious and error-prone, and it gets very old very quickly if you have dozens of repositories.

Thankfully, the Git configuration system is flexible enough to solve this automatically if you can come up with the rules you sort your repositories by.

As a particular example, on my Windows computer, I have the following rules:

This allows me to configure Git using several sets of files. In the main file ~/.gitconfig (aka %USERPROFILE%\.gitconfig), I have the following default values:

    name = Friedrich von Never
    email =
    signingkey = 0x0000000000000000
[includeIf "gitdir:W:/Work/"]
        path =
[includeIf "gitdir:T:/Temp/"]
        path = .gitconfig.temp

And then, in the corresponding files, ~/ and ~/.gitconfig.temp, I can override these defaults, set alternate emails and keys, and so on. Also, I can choose to disable or enable commit signing in some of these configuration files, while keeping the default for the rest.