Set git config values for all child folders

As mentioned by NateEag's edit, git's Conditional Includes are perfect for this. Since that answer's the one for people on git < 2.13, here's one for those who have newer versions.

First, create a new config file somewhere with the settings you want to take effect in the sub-folders - using the original question's folders, let's say it's at ~/topLevelFolder1/.gitconfig_include

In ~/.gitconfig, add:

[includeIf "gitdir:~/toplevelFolder1/"]
    path = ~/topLevelFolder1/.gitconfig_include

Any subfolder of ~/topLevelFolder1 will now include the config in ~/toplevelFolder1/.gitconfig_include - there isn't a need to manually change the .git/config in each subfolder's repo. (This doesn't override whatever's in the subfolder config - it just adds to it, as "include" implies.)

Notes:

  • This setting should be located after the config you want to override in ~/.gitconfig because includeIf will be overridden again by any config that comes after it.
  • This setting includes the file only if you are in a repository under the given path. It's ignored if you're in any non-repository sub-path.
  • The trailing forward slash (/) in the gitdir condition is important.
  • git config --list is good for testing this. You'll see any overrides below includeIf lines in the output. You can also check specific entries with, e.g., git config --get user.email
  • On Git for Windows, specify paths relative to your user directory with ~/ and absolute paths with the Windows-style drive, like C:/ using forward slashes only. Backslashes and Unix-style mount points like /c/ don't work. Furthermore, in the includeIf part, you must specify the path with the correct case as the comparisons are case sensitive.

EDIT: Git 2.13 introduced conditional includes, which are designed to solve this exact problem.

My original answer is preserved below, for history's sake (and users stuck on older versions of git).

====================================

The exact behavior you desire is not supported, based on reading the gitconfig manpage.

However, as of git 1.7.12, Git reads config data from four different sources, two of which are user-specific:

$XDG_CONFIG_HOME/git/config and ~/.gitconfig. Entries in ~/.gitconfig override entries in $XDG_CONFIG_HOME/git/config.

That means you can store your personal gitconfig at $XDG_CONFIG_HOME/git/config and put machine-specific overrides in ~/.gitconfig. Something like

[user]
    email = [email protected]

in ~/.gitconfig should cover your email case.

Note that if $XDG_CONFIG_HOME isn't set git will look for ~/.config/git/config.

This works well for me, since I only ever have two personal repos on work machines (my emacs config and my dotfiles). If you add personal repos to your work machines frequently, this may not be good enough for you.

In that case, custom wrappers around git init and git clone would be your best bet.

Any binary on your $PATH whose name matches 'git-*' can be called as a git command, so you'd just need a pair of shell scripts that call the original command with all passed args, then copy the correct config file into .git/config.

Tags:

Git