/* there’s no place like code */

Set up different work e–mail in git by directory

For some time now git can include additional config files depending on directory you are in (since version 2.13.0 to be precise). Most common use case for this is setting your work e–mail for repositories checked out in one directory (e.g. ~/src/@example-inc) while keeping your private e–mail for the rest of the projects.

There’s a good chance you currently have something similar to this in your ~/.gitconfig:

[user]
    name = "Test “The Face” McTestface"
    email = test@example.net
    # Hopefully you also have a GPG key for the e–mail.
    # Ignore signingKey key here and later if you don’t.
    signingKey = "0123456789ABCDEF"

If you want to use a different e–mail for work stuff, you need to remember to set it every time you create or clone a repository:

git config user.name "Test McTestface"
git config user.email "test.mctestface@example.com"
git config user.signingKey "FEDCBA9876543210"

No more!

You can add a conditional include to your ~/.gitconfig:

[user]
    name = "Test “The Face” McTestface"
    email = test@example.net
    signingKey = "0123456789ABCDEF"

[includeIf "gitdir:~/src/@example-inc"]
    path = ./.gitconfig.work

Then create ~/.gitconfig.work and put any config you want to be used for repositories checked out in ~/src/@example-inc (or its subdirectories):

[user]
    name = "Test McTestface"
    email = "test.mctestface@example.com"
    signingKey = "FEDCBA9876543210"

Presto!


If you are using vim, you may want to add a vim modeline to get proper syntax highlighting in ~/.gitconfig.work:

# vim: ft=gitconfig