Pre-Commit Hook

Git Hook: Pre-Commit for DEBUG Comments

Will Smith sends his regards…

Now this is a story all about how
My project got flipped-turned upside down
And I’d like to take a minute
Just sit right there
I’ll tell you how I made the this git commit hook right here:

You know that feeling you get when you’re writing local code and accidentally commit it, then it makes its way to production somehow?

NO - of course not.

Well, I do. I see it all the time. So, me and the Noblebots were responding to one of these and someone said:

I wish it could automatically detect code I didn’t want to commit and would prevent me from doing that…

To which I said “yeah, that’s a thing”. Thus I created the above pre-commit hook. It’s nothing fancy, or glorious.
I am sure plenty of these exist. But this one is MINE.

You can install it by copying that code into your projects .git/hooks/ folder as a file that’s called pre-commit and make sure you chmod +x pre-commit. Then the next time you are doing test code, just add a comment to it that’s like:

Then when you go to git commit -S -m "[update] Figuring out why this thing isn't working." … because you sign your god damn commits … it will STOP you and give you something like:

BRO! You left DEBUG CODE in your shit. I’m not letting you commit this. Go clean your room: Found references to debug code in file: testfile

A simple fix to a simple problem.
I normally git diff every thing before I commit, and I slowly stage and commit things as needed. This rarely happens if I am that disciplined. But I understand when people need to move fast, these kinds of things make it in. This will help.

You can also install the pre-commit hook globally by making a folder called something like ~/.git_templates then run this: git config --global init.templatedir '~/.git_templates' — from there, any time you clone or initialize a repository, it will add those hooks in that folder to your project. It’s not really “global” but it kind of accomplished the same thing.

Happy debugging!

Leave a Reply