Sub topic 11
Different settings per directory with includeIf
This chapter assumes you can edit ~/.gitconfig, know the difference between global and local scope, and have already set user.name and user.email. You will learn how to load extra configuration only for repositories in certain directories.
Why one global identity is not enough
A global user.email applies to every repository. That is a problem if you use a personal address for open source and a company address for work. Setting the email in each work repository by hand is easy to forget, and a commit made with the wrong address is hard to fix after it has been pushed.
includeIf solves this. It is a section in a config file that says: "include this other file, but only if a condition is true." The condition is checked each time Git runs, so it follows the repository you are in.
The basic pattern
Put your defaults in ~/.gitconfig. Add a conditional include that points at a second file. The second file holds only the settings that differ.
# ~/.gitconfig
[user]
name = Ada Lovelace
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# ~/.gitconfig-work
[user]
email = [email protected]Inside any repository under ~/work/, Git reads ~/.gitconfig-work and its user.email replaces the personal one. Everywhere else, the personal address stays.
The order matters. Git reads the file from top to bottom, and a later value overrides an earlier one. Place the includeIf section after the settings it should override. If you put it above [user], the global email is read last and wins.
How the gitdir condition matches
The gitdir: condition compares the path of the repository's .git directory with a pattern.
- A pattern ending in
/matches everything below that directory.gitdir:~/work/is treated as~/work/**. - A leading
~/expands to your home directory. - Matching is case-sensitive. Use
gitdir/i:for case-insensitive matching, which helps on macOS and Windows. - Without the trailing slash, the pattern matches only that exact directory.
Other conditions exist. onbranch: matches the current branch, and hasconfig:remote.*.url: matches repositories by remote URL. The second is useful when repositories are scattered across directories but share a host.
[includeIf "hasconfig:remote.*.url:[email protected]:acme/**"]
path = ~/.gitconfig-workhasconfig: needs Git 2.36 or newer. Check your version with git --version.
Verify that it works
Do not assume the include is active. Change into a repository and ask Git where the value comes from.
cd ~/work/project
git config --show-origin --get user.emailThe output names the file that supplied the value. If it shows ~/.gitconfig instead of ~/.gitconfig-work, the include did not apply.
git config --show-origin --get-all user.email--get-all prints every value Git read, in order, so you can see both the personal and work addresses and confirm which one is last.
Common errors
- Include not applied. The repository is outside the matched directory, or the pattern lacks a trailing slash. Run
git rev-parse --git-dirto see the real path. - Wrong value wins. The
includeIfsection sits above the setting it should override. - Symlinked directories. Git compares real paths in some cases. If
~/workis a symlink, match the real target or add a second condition. - Path not found. A relative
pathis resolved relative to the file containing the include, not your current directory. Git ignores a missing file without an error, so a typo fails silently.
What else belongs in an included file
Identity is the most common use, but any setting can go in an included file. You might set a different core.sshCommand to use a work SSH key, or turn on commit.gpgsign only for work repositories.
# ~/.gitconfig-work
[user]
email = [email protected]
signingkey = ABCD1234
[commit]
gpgsign = true
[core]
sshCommand = ssh -i ~/.ssh/id_workLocal .git/config is read after global and included files, so a repository-level setting still overrides everything above.
Exercise
Your ~/.gitconfig contains [includeIf "gitdir:~/work"] with no trailing slash, and the work email never applies in ~/work/project. What is the likely cause, and how do you confirm the fix?
Show answer
Without the trailing slash, the pattern matches only the exact directory ~/work, not the repositories below it. Change it to gitdir:~/work/. Then run git config --show-origin --get user.email inside ~/work/project and check that the output names ~/.gitconfig-work.