How To Prevent Unwanted Code From Ending Up In Your Commits

Josh Farrant
2 min readDec 15, 2016
An example of left-over debug code in a commit

Checking the diff of the code that you’re about to commit is a good habit to get into, and it’s something I try do before committing any changes to the project I’m working on.

Inevitably, however, debug code does slip through the cracks. I can’t tell you the number of times I’ve committed and pushed code, only to then spot a rogue IT WORKS! or myVar: 7 popping up in the console. It then takes a second commit to remove the code with some message along the lines of Removed debug code.

This was starting to bother me, so I decided to take action and write a simple pre-commit hook to catch any code that shouldn’t be committed.

#!/bin/shfor FILE in `git diff-index — cached — name-status HEAD — | cut -c3-` ; do
# Check if the file contains ‘XXX’
if grep -q ‘XXX’ $FILE; then
echo ‘Commit failed:’ $FILE ‘contains XXX’
exit 1
fi
done
exit

This code should be added to the .git/hooks directory of your git project and named precommit.

The code above will be triggered every time you try to commit. Before the commit is initiated, it will look through a diff of your changes for the string XXX and, if it finds it, will cause the commit to fail with an appropriate error.

--

--

Josh Farrant

Full stack JavaScript developer based in Birmingham, UK. 👨🏻‍💻🧞‍👨🏻‍✈️⛷