Different emails for different Git repositories

On my laptop I have two directories:

  • ~/activestate, where my work projects reside, and
  • ~/me -- for my personal stuff.

The problem is, when I commit changes I want them to be properly attributed. More specifically:

  1. everything that belongs to ActiveState should be checked in using my work email
  2. all other stuff checks in with my private address
  3. I don't want to `git config user.email <...>` each time I clone a new repository ('cause I do it a lot).

Here's what I've done:

  • in my home directory I created a file called ~/.gitemail containing just my private email. This address is going to be the default
  • to the ~/activestate directory I added another .gitemail with my work address.
  • finally, I added this snippet to ~/.bashrc:
    alias git='GIT_AUTHOR_EMAIL=$(
          p=$(pwd)
          while [[ $p != "$HOME" ]]; do
            [ -e $p/.gitemail ] && cat $p/.gitemail && break
            p=$(dirname $p)
          done) /usr/bin/git'

The alias scans all the directories up to the home dir looking for a file called .gitemail. When found, it sets the GIT_AUTHOR_EMAIL variable to the file's content. This effectively makes the actual git command use the subtree-specific email. Now, when I'm working e.g. in ~/activestate/stackato, it will automatically pick up my work email from ~/activestate/.gitemail.

No extra efforts, less things to remember, and no history rewriting anymore.