de en

Thorsten Reimers

Switch Global Git Configurations

Oct 21, 2023

How to switch between global Git configurations?

I found this great guide: Can I specify multiple users for myself in .gitconfig?

The idea: Simply examine the path of the Git directory in an IncludeIf and use it to include a configuration. You then create different configuration files for different paths and use them to change the committer email address:

~/.gitconfig-work

[user]
     email = worker@example.com

or

~/.gitconfig-personal

[user]
     email = personal@example.org

The IncludeIfs are then added to the central Git configuration file:

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

You can then check the whole thing in a Git directory

git config -l | grep user
user.name=Billy Dean
user.email=worker@example.com

IncludeIf has a powerful syntax (based on gitignore). It allows the use of leading "~", "." or "/". Otherwise "**/" is prepended. A final "/" expands to "/**" (see here).