Skip to content

Instantly share code, notes, and snippets.

@dathanb
Last active June 18, 2024 04:54
Show Gist options
  • Save dathanb/f37a820776d97585ccce811a35c15197 to your computer and use it in GitHub Desktop.
Save dathanb/f37a820776d97585ccce811a35c15197 to your computer and use it in GitHub Desktop.
Git pre-commit hook to stop commit if changes contain sensitive values
#!/usr/bin/env bash
# pre-commit hook to prevent a commit if the commit includes changes that
# match a set of stop strings
FORBIDDEN='DO NOT COMMIT'
git diff --cached | grep -q -E "${FORBIDDEN}"
ret=$?
if [[ $ret = 0 ]]; then
echo "COMMIT REJECTED\nFound changes containing forbidden \"${FORBIDDEN}\" references. Please remove them before committing"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment