Skip to content

Instantly share code, notes, and snippets.

@mrabbitt
Last active December 23, 2015 17:09
Show Gist options
  • Save mrabbitt/6666720 to your computer and use it in GitHub Desktop.
Save mrabbitt/6666720 to your computer and use it in GitHub Desktop.
Git pre-commit hook to prevent commits which change lines with a specific substring.
#!/bin/sh
# Released as public domain by Michael Rabbitt (github.com/mrabbitt)
#
# Suppress commits with a specific substring in changed connect.
#
# Example (to suppress commits of lines with string DONTCOMMIT):
#
# git config hooks.dontcommit DONTCOMMIT
#
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
dontcommit=$(git config hooks.dontcommit)
# Redirect output to stderr.
exec 1>&2
if [ -n "$dontcommit" ] &&
[ $(git diff -U0 --cached $against | grep -E '^(\+|-)' | grep -c ${dontcommit}) != 0 ]
then
cat <<EOF
You attempted to commit one or more changes containing the string
'${dontcommit}'. You can disable this check using:
git config --unset hooks.dontcommit
EOF
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment