Skip to content

Instantly share code, notes, and snippets.

@r-k-b
Created March 11, 2019 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-k-b/173a0951cf49ee5e26795f86985db24a to your computer and use it in GitHub Desktop.
Save r-k-b/173a0951cf49ee5e26795f86985db24a to your computer and use it in GitHub Desktop.
Prevent accidental commits of files

copy pre-commit into the repo's .git/hooks/ folder, then try committing a file that has the string DO_NOT_COMMIT_THIS in it.

#! /bin/sh -e
disallowed="DO_NOT_COMMIT_THIS"
git diff --cached --name-status | while read x file; do
if [ "$x" == 'D' ]; then continue; fi
for word in $disallowed
do
if egrep $word $file ; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
exit 1
fi
done
done || exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment