Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created February 15, 2023 04:41
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 luckylittle/0948a82d5db1f058dc18d534a500a0fc to your computer and use it in GitHub Desktop.
Save luckylittle/0948a82d5db1f058dc18d534a500a0fc to your computer and use it in GitHub Desktop.
Pre-commit Git hook (.git/hooks/pre-commit.sh) that checks certain keywords
#!/bin/sh
matches=$(git diff-index --patch HEAD | grep '^+' | grep -Pi 'password|keyword2|keyword3')
if [ ! -z "$matches" ]
then
cat <<\EOT
Error: Words from the blocked list were present in the diff:
EOT
echo $matches
exit 1
fi
@luckylittle
Copy link
Author

luckylittle commented Feb 15, 2023

Scripts must be named to match the related events (Git 2.x):

  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-rebase
  • post-checkout
  • post-merge
  • pre-receive
  • update
  • post-receive
  • post-update
  • pre-auto-gc
  • post-rewrite
  • pre-push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment