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?

  • My first reflex is to hack the scripts to tweak .gitconfig in each directory they create. For example, IIRC Android's repo can do this, but you have to read the source carefully to find out. (I'm not completely sure, I haven't done that in a while.) – Gilles 'SO- stop being evil' Jul 11 '12 at 22:37
  • @Gilles: That's certainly a possibility. The repos in question are the package repository for a Linux distro that has recently migrated from CVS to Git. We're still working on redoing all our tooling. In the long term that's probably where this gets fixed, but in the short term those of us working on it are experimenting with what the options are. – Caleb Jul 12 '12 at 9:04
  • See answer by @gik - this uses a native Git feature that enables this sort of multi-level config based on shared local pathname for subset of repos. – RichVel Jul 27 at 8:47

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
            command git -c user.email=alternate@credentials.org "$@"
            command git "$@"
| improve this answer | |
  • can I do command git -c user.email=alternate@credentials.org user.name="Alter Ego" "$@" or how should I go about that? Searched high & low and the only reference to this -c flag I found was yours, thanks, much appreciated. – Vic Goldfeld Jan 28 '13 at 17:36
  • for the record, I got it working with command git -c user.email=alternate@credentials.org -c user.name="Alter Ego" "$@" – Vic Goldfeld Jan 28 '13 at 18:07

You can configure the email address for git with the environment variable GIT_AUTHOR_EMAIL. If you combine this with Execute bash scripts on entering a directory or directory specific shell configuration with zsh you can easily change the settings per directory or parent directory, e.g. if you enter into a directory in ~/work you can automatically adjust the environment variables to change your email address.

| improve this answer | |

See the solution based on git configuration:


Add to “~/.gitconfig” file:

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

Create “~/.gitconfig-project1″ file with content:

      sshCommand = "ssh -i ~/.ssh/project1 -F /dev/null"  

      name = user1  
      email = user1@email.com  
| improve this answer | |

Based on Caleb's answer, we can define a modified git command which permanently configures all of the repositories in this directory correctly, so that all future uses of vanilla git will use the new configuration. I use hub, which is another git wrapper, so I replaced my alias git=hub with this and called hub in my function - if you don't use hub, replace all hub invocations with command git:

function git() {
    case "$PWD" in
            hub "$@"
            # We don't know which repository was cloned / operated on, so let's just reconfigure them all
            for f in *; do
                [[ -d "$f" ]] && hub -C "$f" config user.email $my_private_email
            hub "$@"
            hub config user.email $my_private_email
            hub "$@"

Unlike Caleb's answer, which only works in a shell (and unless explicitly sourced, only in an interactive shell), this also affects other git front-ends which correctly read the git config, such as emacs magit.

| improve this answer | |

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?

Yes, use git config conditional includes:

; include for all repositories inside $HOME/to/group
[includeIf "gitdir:~/to/group/"]
    path = /path/to/foo.inc

Note that the last slash at the end of the gitdir is needed.

To check that it works:

cd ~/to/group/some-checkout
git config --show-origin
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.