Skip to content

Instantly share code, notes, and snippets.

@pestophagous
Last active September 14, 2015 21:33
Show Gist options
  • Save pestophagous/8783885e327042d047a0 to your computer and use it in GitHub Desktop.
Save pestophagous/8783885e327042d047a0 to your computer and use it in GitHub Desktop.
rename this to .git/hooks/pre-commit inside your git repo to prevent accidental commits that you tagged as DNC (do-not-commit)
#!/bin/sh
#
# https://twitter.com/pestophagous/status/501460327515815936
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
# DNC stands for 'do not commit'
MARKER_STRING_TO_PREVENT_A_COMMIT="DNC DNC DNC"
COUNT_BAD_DISALLOWED_LINES=`git diff --cached $against | grep "${MARKER_STRING_TO_PREVENT_A_COMMIT}" | wc -l`
if [ "$COUNT_BAD_DISALLOWED_LINES" -eq "0" ]
then
# don't do anything. we are happy so far.
true
else
echo "custom pre-commit script is BLOCKING this commit! You have \"$MARKER_STRING_TO_PREVENT_A_COMMIT\" (do not commit) in your source file!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment