Skip to content

Instantly share code, notes, and snippets.

@Fifirex
Last active June 24, 2021 12:21
Show Gist options
  • Save Fifirex/e82b2a8107edfd1ffcaf38adeb4caacb to your computer and use it in GitHub Desktop.
Save Fifirex/e82b2a8107edfd1ffcaf38adeb4caacb to your computer and use it in GitHub Desktop.
Reminds you that a specific line should not be committed.
#!/bin/bash
#
# This hook will look for code comments marked '##no-commit'
# - case-insensitive
# - dash is optional
# - there may be a space after the ##
#
noCommitCount=$(git diff --no-ext-diff --cached | egrep -i --count "(@No|\#\#\s?no[ -]?)commit")
if [ "$noCommitCount" -ne "0" ]; then
echo "WARNING: You are attempting to commit changes which include a 'no-commit'."
echo "Please check the following files:"
git diff --no-ext-diff --cached --name-only -i -G"(@no|\/\/s?no-?)commit" | sed 's/^/ - /'
echo
echo "You can ignore this warning by running the commit command with '--no-verify'"
exit 1
fi
@Fifirex
Copy link
Author

Fifirex commented Jun 24, 2021

Add this to your project by moving the file to .git/hooks and running the following to make it executable:

$ cd .git/hooks
$ chmod +x pre-commit

Credits to this answer for the script.

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