Shell – Can git configuration be set across multiple repositories

gitshell

Git seems to support configuration values at three levels:

  • Per-system global settings (stored in /etc/git-core)
  • Per-user global settings (stored in ~/.gitconfig)
  • Per-repository local settings (stored in $REPO/.git/config)

These options cover most of the basis but I'm looking for a way to handle a fourth level. I have a (very) large collection of repositories for which I need to use a different value for user.email than my usual. These repositories are often created and manipulated through automated scripts, and setting up per repository local settings is cumbersome.

All of the repositories in question are located under a certain path prefix on my local system. Is there a way to set a configuration value somewhere that will be inherited by all repositories under that path? (Sort of like .htaccess settings inherit all the way up the file system.) Perhaps there would be a way to set conditional values in the global config file? What other arrangements could be made in a UNIX environment to cope with a set of repositories like mine?

Best Answer

I have found no way to configure git at this fourth level. The only way seems to be per-command configuration value overrides using git -c key=value.

My current hacky solution is to define a shell function that serves as a wrapper for git. When called, it passes the arguments onto the system git command, but not before checking on the present working directory and adding an extra argument to the command if applicable.

function git () {
    case "$PWD" in
        /path/to/repos/*)
            command git -c user.email=alternate@credentials.org "$@"
            ;;
        *)
            command git "$@"
            ;;
    esac
}
Related Question