Git: pre-commit hook to find common debug functions such as console.log(), var_dump() and print_r()

Git: pre-commit hook to find common debug functions such as console.log(), var_dump() and print_r()

Posted by on Feb 24, 2014 in Git | 1 comment

Accidentally committing debug messages is never fun. Here is a quick Git pre-commit hook to search for common debug functions such as console.log, var_dump, print_r and git conflict merge errors

Copy the following into a file called pre-commit (yes it does not have an extension). This file needs to reside in your repository under .git/hooks:

FORBIDDEN= is a regular expression that you can modify to see fit, I have also included <<<<<<<, =======, >>>>>>> which are left behind by bad conflict merges. If you use any of them as comments you might need to change your commenting style to use something unique like +++++++++

Now every time you commit, this will run and check for the offending code. If found a printout will tell you the file and line number of the offender and reject the commit. If you do mean to have the debug code present you can always force the commit using git commit --no-verify. Thankfully there is a little helper message reminding you of that if your commit is rejected.

More on Git hooks

    1 Comment

  1. Hi Kate,

    Thanks for this, this is the most straight up pre-commit hook example I’ve come across. Can I ask if this is for only staged files?

    Best, Isa
    @msherico

Post a Reply

Your email address will not be published. Required fields are marked *