Conditional Git Config With includeIf

Switch user identity or settings based on which directory a repo is in.

macOSgitcliconfigLow risk
Use includeIf."gitdir:~/work/".path to load a work config only inside that tree.

01Command

Command
$ git config --global includeIf."gitdir:~/work/".path ~/.gitconfig-work

02What it does

Conditional includes let one machine carry several identities, automatically applying the right name, email and signing key depending on where the repository lives. This prevents committing with the wrong identity, a problem purely handled by configuration from the shell.

03Risk

RiskLow risk
RequirementsGit 2.20+ (install with Homebrew or Xcode Command Line Tools)
CompatibilitymacOS with Git installed (Homebrew or Xcode Command Line Tools)
!Warning: Paths must end with a trailing slash for gitdir patterns; test with git config --list --show-origin.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer