index all rss twitter github linkedin email
Álvaro Ramírez

Álvaro Ramírez

30 April 2018 Pre-commit hooks to save you from yourself

Wanted to try out some code, but needed to ensure never checked in. Git pre-commit hooks are handy in this space. Add the following script to search for either @COMMITFAIL or @NOCOMMIT in the staged files. If found, attempts to commit will fail.

Based on


set -o nounset
set -o errexit

echo "Arguments:"
echo "$@"
echo "---"

readonly FILES_PATTERN='(\..+)?$'

if ( git diff --cached --name-only | grep -E "$FILES_PATTERN" | xargs grep -E --with-filename -n "$FORBIDDEN" ); then
  echo "ERROR: @COMMITFAIL or @NOCOMMIT found. Exiting to save you from yourself."
  exit 1

Save to a file and create a symbolic link to your .git/hooks directory:

ln -s ../../git/ .git/hooks/pre-commit