gitconfig includeIf checker · guide

Why is git using the wrong email? Six ways includeIf fails

The usual setup keeps a personal address in ~/.gitconfig and switches to a work address, signing key or SSH command for repositories under ~/work/ with an [includeIf] block. When it fails, git says nothing: the condition is false, or the file is missing, or a later line wins, and you find out from a commit with the wrong name on it. Every case below was run against git 2.43; the checker finds all six from your pasted files.

First, ask git where the value came from. Inside the repository:

git config --show-origin --get-all user.email
git rev-parse --absolute-git-dir      # the path gitdir: is compared with

The last line printed is the value git uses.

1. No trailing slash

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

gitdir: is compared with the repository's .git folder, for example /home/sam/work/api/.git. ~/work becomes /home/sam/work, which has to match that whole path, and does not. A trailing slash makes git add **: gitdir:~/work/ becomes /home/sam/work/** and matches every repository below it. The same trap catches gitdir:~/work/api: that names the work tree, not its .git folder. Write gitdir:~/work/api/.

2. The include comes before the setting it should override

[includeIf "gitdir:~/work/"]
	path = ~/.gitconfig-work
[user]
	email = sam@personal.example

The include is read where it is written, and later values win. Here the work address is read first and the personal one replaces it. git config --show-origin --get-all user.email shows both, the work one first. Move every [includeIf] block to the end of ~/.gitconfig.

3. A condition git does not know

The prefixes are case-sensitive and exact: gitdir:, gitdir/i:, onbranch: and hasconfig:remote.*.url:. [includeIf "GitDir:~/work/"] is an unknown condition, and git treats unknown conditions as false without a warning. So is hasconfig:remote.origin.url:: only the literal remote.*.url is supported. A space is kept as part of the pattern, so [includeIf "gitdir: ~/work/"] starts with a space, no longer starts with ~, gets **/ put in front and matches nothing.

4. The file is not where the path says

If the file named by path = does not exist, git skips it silently. A relative path is relative to the file the line is in, not to your current folder, and git does not expand variables: path = $HOME/.gitconfig-work looks for a folder literally called $HOME. Use ~/. A key other than path under [includeIf] (file =, paths =) is stored and ignored.

5. The folder is reached through a symlink

If ~/work is a link to /mnt/data/work, gitdir:~/work/ works in some places and not others. git first tries the real path, /mnt/data/work/api/.git, then the path as typed, but it only knows the typed path when it runs in the top folder of the repository from a shell that changed into it through the link. We checked this with git 2.43: in the top folder the include applied; in a subfolder, and with git -C, it did not. Editors, hooks and GUIs usually see the real path. Add a second block for the real location:

[includeIf "gitdir:/mnt/data/work/"]
	path = ~/.gitconfig-work

6. onbranch: on a detached HEAD

onbranch: compares the short branch name (main, not refs/heads/main; a trailing slash matches everything below, so onbranch:feature/ covers feature/a/b). With no branch checked out, which is how most CI systems check out a commit or a pull request and what happens during git bisect or a rebase, every onbranch: include is skipped. Settings CI depends on belong in an unconditional file.

Two more that surprise people

Linked worktrees. A worktree made with git worktree add ~/scratch/fix keeps its .git folder inside the main repository, at ~/work/api/.git/worktrees/fix. gitdir: sees that path, so where the main repository lives decides, not where the worktree is.

A broken line stops every command. A syntax error in an included file (an unclosed quote, a [section with no bracket, an unknown escape like \q) makes every git command in a matching repository fail with fatal: bad config line N in file F. An include chain more than ten files deep, a file that includes itself among them, fails with exceeded maximum include depth.

Questions

Why does includeIf gitdir:~/work not match my work repositories?

Without a trailing slash the pattern has to equal the whole path of a repository's .git folder, so gitdir:~/work only matches a repository whose .git folder is ~/work itself. Write gitdir:~/work/ and git adds ** after the slash, which matches every repository below ~/work.

Why does git use my personal email even though the work include matched?

Because git reads ~/.gitconfig from top to bottom and the last value of user.email wins. If the [includeIf] block comes before your [user] section, the personal address is read after the work one and replaces it. Put the [includeIf] blocks at the end of the file.

How do I see where git got user.email from?

Run git config --show-origin --get-all user.email inside the repository. It prints every value with the file it came from, in reading order; the last line is the one git uses. git config --list --show-origin --show-scope shows everything.

Check your own files with the gitconfig includeIf checker. It runs in your browser and nothing is uploaded.