Skip to content

Instantly share code, notes, and snippets.

@ivan4th
Last active June 5, 2025 20:17
Show Gist options
  • Save ivan4th/c9d5cb128d82363ee402f08f45b28ac5 to your computer and use it in GitHub Desktop.
Save ivan4th/c9d5cb128d82363ee402f08f45b28ac5 to your computer and use it in GitHub Desktop.
Avoid committing incomplete code and debug print
$ mkdir ~/.git-hooks

Place commit-msg and pre-commit hooks into that dir and make them executable:

$ chmod +x ~/.git-hooks/commit-msg
$ chmod +x ~/.git-hooks/pre-commit

Make git use the hooks:

$ git config --global core.hooksPath ~/.git-hooks

After that, you will not be able to commit any files that contain QQQQQ: substring unless you also include wip word (any case) in the first line of your commit message.

So, if you have written comments and debug print like this

// QQQQQ: remove this!!!
...
printf("QQQQQ: foo=%s\n", foo);

and forgot about them, an attempt to git commit your changes will remind you unless you're making a wip commit.

NOTE: ${firstline,,} downcase construct doesn't work with bash 3.2, so on Mac, you need to have newer bash installed somewhere in your path. That's what #!/usr/bin/env bash instead of #/bin/bash is for. If you need this to work with bash 3.2, use $(echo firstline | tr A-Z a-z) or firstline=$(head -n1 "$1"| tr A-Z a-z) instead.

#!/usr/bin/env bash
r=0
if [ -f .git/need-wip-override ]; then
rm -f .git/need-wip-override
firstline=$(head -n1 "$1")
if [[ ! "${firstline,,}" =~ (^| )wip( |$) ]]; then
echo "Error: Commit contains files with 'QQQQQ:' but commit message is missing 'wip' in first line."
r=1
fi
fi
exit ${r}
#!/usr/bin/env bash
set -o pipefail
files=$(git diff --cached --name-only)
rm -f .git/need-wip-override
for file in ${files}; do
if git show :$file | grep -n 'QQQQQ:' | sed "s@^@${file}:@"; then
touch .git/need-wip-override
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment