Git: Require Work Item # (Or Similar) In Commit Messages

So here at Videa, we require that Git commits have the TFS Work Item # in the Commit message for linking the work to the Work Item.

In order so that I don’t forget ever again, I wrote a commit-msg hook in Git to keep me honest.

Save the following as “.git/hooks/commit-msg” in your Git Repo. This works on Windows and *nix with any tool that uses the git command line (SourceTree does, however other tools are YMMV)

#!/bin/bash

grep -EHnq "^((Fixes|Resolves|PBI|Bug|Task|Work Item|WI): (\#([0-9])+|None)|Merge branch '(.*)' into)+" "$1"; RET=$?
if [ $RET -eq 0 ]; then
    exit 0
fi

echo "ERROR: Work Item # missing in commit message" 1>&2
exit 1