Different Git Configs per Folder

How to use includeIf in your .gitconfig to switch identities based on the workspace folder.

Show config

Sometimes you want different emails or signing keys for different projects – for example to separate work and private repositories. Instead of setting them in every repository, let Git pick the right config automatically with the includeIf directive.

Switch config by folder

If all your projects live under ~/workspace but everything in ~/workspace/acme/ should use your work identity:

File: ~/.gitconfig

[user]
    email = [email protected]
    name = Mike
    signingkey = 123ABCDEF456
[commit]
    gpgsign = true

[includeIf "gitdir:~/workspace/acme/"]
    path = .gitconfig-acme

File: ~/.gitconfig-acme

[user]
    email = [email protected]
    name = Mike Petersen
    signingkey = 789ABCDEF456

The trailing slash matters: gitdir:~/workspace/acme/ matches every repository below that folder. Settings from the included file override the ones above it, so you only need to list what differs.

Switch config by remote URL

Since Git 2.36 you can also match on the remote URL instead of the folder. That's handy if your repositories aren't sorted by client:

[includeIf "hasconfig:remote.*.url:[email protected]:acme/**"]
    path = .gitconfig-acme

Signing with SSH instead of GPG

Git can also sign commits with an SSH key, which many people already have anyway:

[gpg]
    format = ssh
[user]
    signingkey = ~/.ssh/id_ed25519.pub
[commit]
    gpgsign = true

Check which config is active

Inside a repository, this shows the effective value and the file it came from:

git config --show-origin user.email