Git includeIf: Different Email per Folder (and the Traps)
Use a conditional include: add an [includeIf "gitdir:~/work/"] block to ~/.gitconfig that points to a second config file, and put the work email in that file. Every repo under ~/work/ then commits with the work email, with no per-repo setup.
It takes about two minutes. It also has four classic traps, covered below.
How do I set a different git email per folder?
Git 2.13 added conditional includes. Here is the whole setup.
Your global ~/.gitconfig keeps the default identity and adds one include per folder:
[user]
name = Ada Dev
email = ada@personal.dev
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
Then ~/.gitconfig-work holds only what changes:
[user]
email = ada@company.com
Check it from inside any repo:
cd ~/work/some-repo
git config user.email
It should print ada@company.com. Anywhere else, you get the personal address.
Why does includeIf need a trailing slash?
A gitdir: pattern that ends with / matches every repository below that folder. Without the slash, it matches only that exact path. This is the most common reason “it works in one repo and not in the others”. Write gitdir:~/work/, not gitdir:~/work.
Why is my includeIf ignored?
Work through this list:
- Wrong order. An include is applied at its position in the file. Keep it below the default
[user]block, or the default overrides it. - You are not inside a repo.
gitdir:matches the.gitdirectory of the current repository. Run the check from inside a repo, not from a plain folder. - Case or path mismatch. On macOS, use
gitdir/i:if you are not sure about the case of the folder name. - Symlinks. If
~/workis a symlink, match the real path.
To see which file a value comes from, run:
git config --show-origin user.email
Can includeIf match a GitHub organization instead of a folder?
Yes, since Git 2.36. hasconfig:remote.*.url: matches on the remote URL:
[includeIf "hasconfig:remote.*.url:git@github.com:acme-inc/**"]
path = ~/.gitconfig-acme
This catches a repo wherever you cloned it, as long as it has a matching remote. A freshly initialized repo with no remote yet matches nothing, so the default identity applies until you add one.
What about the SSH key, gh and commit signing?
The email is only one of four layers. The same included file can also set:
[user]
email = ada@company.com
signingkey = ~/.ssh/id_work.pub
[commit]
gpgsign = true
[gpg]
format = ssh
[core]
sshCommand = ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes
IdentitiesOnly=yes stops ssh from offering every key in your agent, which is how pushes end up authenticated as the wrong account. See Git push permission denied to the wrong GitHub account for that failure in detail.
What includeIf cannot do is switch the gh CLI session. gh auth switch is separate, so a pull request can still open from the wrong account. If your commits show a gray badge, the signing layer is the culprit: GitHub unverified commits with multiple accounts explains the fix.
What happens when I clone outside my folders?
Nothing matches, so the default identity is used. A quick test clone in /tmp leaves with your personal email, and nothing warns you. This is the hole in a pure includeIf setup. Hats closes it: it writes plain includeIf rules for you, switches the SSH key, gh and signing together, and its guard blocks a commit or push when the folder’s hat does not match. The full picture is in Multiple GitHub accounts on one Mac, and Hats is free for two profiles.
brew install --cask xorbeta/hats/hats
hats rule add --folder ~/work work
FAQ
Which git version do I need for includeIf?
Git 2.13 or later for gitdir:. The hasconfig:remote.*.url: condition needs Git 2.36. Run git --version to check.
Does includeIf work with git worktrees?
It matches on the git directory of the current repository, so check with git config --show-origin user.email from inside the worktree rather than assuming.
Do I have to rewrite old commits after fixing my email?
The fix applies to new commits only. Old commits keep the email they were made with. Rewriting history is a separate decision, and it changes commit hashes.
How do I list every identity setting git is using?
Run git config --list --show-origin. Each line shows the file it came from, which makes a wrong include easy to spot.