Using Git commit hooks to prevent stupid mistakes Apr 20 2011

I’m pretty prone to making stupid mistakes all the time. I think its a mixture of being busy, and being a natural scatter brain. In any case, mistakes happen, and thankfully over time developers have come up with a number of ways to combat mistakes. These usually take the shape in automated toolds. Unit tests are a great way to automated tool to help prevent stupid mistakes from happening, Git commit hooks are another. Git hooks are shell or other scripts that you install in your repo, and git will run them at certain points in time. One nice advantage of git commit hooks is you can install them locally and you can avoid publishing the stupid mistake you might make.

For me, I quite often commit debugger; statements in my javascript code. I want to not do that anymore so I created the following hook in .git/hooks/pre-commit

#!/bin/sh if git-rev-parse --verify HEAD >/ dev / null 2 >& 1 ; then against =HEAD else against =4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi for FILE in ` git diff-index --check --name-status $against -- | cut -c3- ` ; do # Check if the file contains 'debugger' if [ "grep 'debugger' $FILE " ] then echo $FILE ' contains debugger!' exit 1 fi done exit