Antoine Lehurt

Managing multiple Git configurations with [includeIf]

I often need to juggle between different Git settings depending on the type of project. For instance, I need to use a different email address and signing key when committing. Luckily, Git has a feature called [includeIf] that helps me manage multiple Git configurations with ease.

For example, I store my work projects in ~/workspaces/work and my personal projects in ~/workspaces/personal. In my ~/.gitconfig, I added the following [includeIf] statements:

[includeIf "gitdir:~/workspaces/work/"]
  path = ~/.config/git/work-config

[includeIf "gitdir:~/workspaces/personal/"]
  path = ~/.config/git/personal-config

# ...all other Git settings

This tells Git to include the configuration file at the specified path when I’m working in the matching directory.

For example, in ~/.config/git/personal-config, I set my GitHub email address and signing key:

[user]
  email = kewah@users.noreply.github.com
  signingkey = ssh-ed25519 AAAAweriaksjldhfkjasbvlkasbv

Now, when I’m working in the ~/workspaces/personal directory, Git automatically uses the [user] settings in ~/.config/git/personal-config.