What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Git’s includeIf lets you load settings from another config file only when a condition matches—for example, using a work email in repositories under ~/work/ and a personal email elsewhere. Put an [includeIf "condition"] section in a Git config file, set its path, then verify the effective value from a repository with git config --show-origin.

What includeIf does

includeIf conditionally includes directives from another Git configuration file. It is a Git configuration feature, not a shell conditional. If the condition is true, Git reads the included file at the point where the include appears in the configuration stream; if it is false, Git may ignore that section. See the Git configuration manual.

The basic form in your global ~/.gitconfig is:

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

The condition goes in quotes after includeIf. The path names the file to load. A path beginning with ~/ expands from your home directory; a relative path is resolved from the config file containing the include directive.

Choose a condition that matches how you organize repositories

Git offers conditions based on the repository’s Git directory, worktree location, current branch, or remote URL. The right one depends on which property should determine the setting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Condition What it matches Use it when
gitdir: The location of the repository’s .git directory, using Git glob rules. Settings should follow where repositories are stored, such as separate work and personal folders.
gitdir/i: The .git directory location, matched without case sensitivity. Path capitalization can vary, especially on a case-insensitive file system.
worktree: The worktree location. Settings should follow the checkout location rather than the Git directory.
worktree/i: The worktree location, matched without case sensitivity. The checkout path may vary in capitalization.
onbranch: The currently checked-out branch name. Settings should apply to a branch or branch namespace, not to a repository directory.
hasconfig:remote.*.url: Whether at least one remote URL matches the supplied glob. Settings should follow a remote host or URL pattern.

Match repositories by directory with gitdir:

A pattern ending in / gets recursive ** behavior. Thus, gitdir:~/work/ can match Git directories below ~/work/, not just a directory named exactly work. Git documents path-matching details, including that outside $GIT_DIR both symlink and real-path forms can match, while .. is treated literally rather than normalized. Avoid writing a pattern that assumes Git will resolve .. for you.

Use gitdir/i: instead if directory capitalization might differ. Both conditions match the Git directory, not necessarily the visible checkout path.

Match by worktree, branch, or remote

Use worktree: when the checkout’s location is the deciding factor, including cases where that is more useful than the location of the .git directory. Add /i to make a worktree pattern case-insensitive.

onbranch: checks the checked-out branch. A trailing slash matches a branch namespace recursively, so onbranch:topic/ covers names such as topic/one. This selects by branch, not by repository directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

hasconfig:remote.*.url: checks whether a remote URL matches a Git glob. Git performs a look-ahead scan for this condition. A file included through a hasconfig condition cannot define remote URLs itself; that restriction prevents the included file from creating the very remote match used to select it.

Set different work and personal commit identities

Set your name globally, then include a small identity file for each repository location. For example, add this to ~/.gitconfig:

[user]
    name = Your Name
    useConfigOnly = true

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

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

Put the work email in ~/.gitconfig-work:

[user]
    email = you@company.example

Put the personal email in ~/.gitconfig-personal:

[user]
    email = you@example.net

Here, useConfigOnly = true prevents Git from guessing an identity when no configured email is available. The directory patterns must match the actual locations of the repositories’ Git directories. Adjust the paths if your checkouts live elsewhere.

Select settings by remote URL instead

If company repositories are not all stored in one directory, match their remote URLs. These two conditions cover common HTTPS and SSH URL forms for repositories under the company path on GitHub:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
[includeIf "hasconfig:remote.*.url:https://github.com/company/**"]
    path = ~/.gitconfig-company

[includeIf "hasconfig:remote.*.url:git@github.com:company/**"]
    path = ~/.gitconfig-company

Git globbing applies to the URL pattern. The included ~/.gitconfig-company must not define remote URLs, because files selected by hasconfig are not permitted to introduce them.

Apply settings to a branch namespace

For a setting that should apply whenever the current branch is under release/, use:

[includeIf "onbranch:release/"]
    path = ~/.gitconfig-release

Place the settings to apply in ~/.gitconfig-release. A branch-based condition follows the checked-out branch; it does not pick a repository based on its path.

Check which configuration Git actually uses

  1. List global entries and their source files: git config --global --list --show-origin. Check that Git sees the config file containing your include directive.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  2. From the target repository, inspect the effective email and its origin: git config --show-origin --get user.email. The reported origin helps identify which file supplied the value.

  3. Check the repository’s Git directory with git rev-parse --git-dir, then compare that location with your gitdir: pattern. Remember that the condition matches the Git directory, not simply the path you expect to see for the checkout.

  4. If needed, temporarily put a distinctive test value in the included file and check whether it appears. Remove the test value when done.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why a conditional include may not apply

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.