Manage emails in git

If you are like me, you tend to work on open source using your personal email and at YP using your enterprise email. But git doesn’t handle that well so you need to never forget to do

git config user.email henri.tremblay@somewhere.com

after each git init or git clone.

I finally had the time for work on an almost satisfying solution.

First, there is a concept of git template. It means that your .git directory will be initialized with files coming from a template. By default, it uses the default template $GIT_INSTALL_DIR/share/git-core/templates.

However, you can define your own. That’s what I did.

If you clone (and fork) this repository, you can then set it as the default template

git config --global init.templatedir "$PWD/template"

Why do I need that? Because I want a standard pre-commit hook that will make sure my personal email is not used when committing to my corporate git repositories and that my work email is never used on GitHub.

Look at the code of the hook. I’m pretty sure that is a lot of room for improvement. But it’s a good start. Of course, you will need to modify the enterprise_email variable to fit yours. If you find a way to improve, please do a pull request.

Some details about what it does

  1. It checks that a remote origin is configured and fails if not. I might found that annoying soon. We’ll see. I need a remote origin because I need to know if it will commit to my enterprise git or somewhere else
  2. It checks is the remote origin is my enterprise and then if the email set is my enterprise email
  3. If not, it sets it and fail. I would have love to commit right away but it seems git keeps committing with the old value.
  4. If I am committing on a repo outside my company, it makes sure I’m not using the enterprise email and fails if I do