Different .gitconfig for a given subdirectory?

I use two different git emails, one for work and one for public projects. Initially I thought that I could create a separate .gitconfig with a different email in a folder where all my public repos are in, and that git would respect that, but alas it seems that doesn't work. What's the best way to easily setup something similar? I want to avoid having to specifically change the email in each public repo.


Solution 1:

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]

Solution 2:

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]