Different .gitconfig for a given subdirectory?

The best way to do this since git 2.13 is to use Conditional includes.

An example (copied from an answer here):

Global config ~/.gitconfig

[user]
    name = John Doe
    email = [email protected]

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

Work specific config ~/work/.gitconfig

[user]
    email = [email protected]

As mentioned in other answers you can't really set your credentials per directory. But git allows you to do so on a per repository basis.

# Require setting user.name and email per-repo
$ git config --global user.useConfigOnly true

This way, on your first commit you will get an error asking you to provide name and email. In your repo folder add your credentials once to your repo and from then on you'll commit with this identity:

$ cd path/to/repo
$ git config user.name My Name
$ git config user.email [email protected]

I have the exact same problem. As a temporary solution, I have this in my .bashrc:

alias git='GIT_AUTHOR_EMAIL=$(
      p=$(pwd)
      while [[ $p != "$HOME" ]]; do
        [ -e $p/.gitemail ] && cat $p/.gitemail && break
        p=$(dirname $p)
      done) GIT_COMMITTER_EMAIL=$(
      p=$(pwd)
      while [[ $p != "$HOME" ]]; do
        [ -e $p/.gitemail ] && cat $p/.gitemail && break
        p=$(dirname $p)
      done) /usr/bin/git'
alias g=git

This way I've got two different .gitemail files in the parent directories:

  • ~/work/.gitemail
  • ~/github/.gitemail

Note that I'm only switching user.email this way, everything else is centralized in ~/.gitconfig. It's a solution, but it's not great.

Hoping someone on StackOverflow has a better idea...