Skip to content

Instantly share code, notes, and snippets.

@kolypto
Created November 10, 2023 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolypto/59975df841e4c25dd9cf178180ce32d8 to your computer and use it in GitHub Desktop.
Save kolypto/59975df841e4c25dd9cf178180ce32d8 to your computer and use it in GitHub Desktop.
Git pre-commit hook: ban "nocommit" lines, do not allow to commit
#!/bin/bash
# .git/hooks/pre-commit
# Find "//nocommit" lines in the diff, do not allow them
# This line will output all "nocommit" lines, if any: you'll see this as git error.
git diff --no-ext-diff --cached | grep -iE '//\s*nocommit'
# If the retcode=0, then "nocommit" lines were found. Fail on them.
if [[ $? -eq 0 ]]; then
echo "WARNING: You are going to commit some 'nocommit' lines!"
echo
echo "You can ignore this warning by running the commit command with '--no-verify'"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment