What Are Git Commit Hooks and What Can They Do for Us?
Some useful examples from real life
What Is a Commit Hook?
Commit hooks (more specifically, pre-commit hooks) are actions that happen right before you commit your changes in a repo. At least that’s how we use them. Apparently, you can also hook into other Git events for various other use cases.
Practical Use Cases
Sort imports alphabetically
This seems small, but it’s nifty and something that I came across only recently while reading up on Python. The isort library and this gist illustrate the concept in more detail. Personally, I don’t need this. My main job is mostly in C# and the ever so wonderful ReSharper does that automatically when you format your code — plus it removes unused imports. But still, pretty nifty.
Forbid unwanted files from being pushed to the repo
This can include files that contain passwords, API keys, or any other secrets, but the most useful case for me was a particular server-side project. To test it locally, the debugging system would create a license file each time a particular file type was touched — and this license file was different from the one on the server. That meant that each time someone forgot and accidentally pushed that file to the repo, we would have to log into the server, uninstall the old certificate, restore it from a backup, switch out the file, boot the server back up, and remove the file from the repository. That was a good 10–20 minutes of totally wasted time. Plus the ten minutes it would cost to get cookies for everyone. I miss those cookies.
Forbid coworkers from pushing changes to a repo
OK, this is a joke, but it’s one that we all found hilarious. When a coworker of ours returned from a hiatus, we implemented a pre-commit hook that forbade any pushes containing their username. For a full day, they couldn’t push anything. We also coupled their monitors to the wall socket that could be turned on and off with a switch by the door — hilarious as well. Good times.
Complain if images are too large
This is very useful when working on website projects. I remember this was causing me so many rebuilds while working with Jekyll, React, and such. You upload a new image and forget to optimize it. The page load speed goes crazy as the client tries to load 10mb of smartphone pictures.
I have not built a hook like this yet, but this piece of magic apparently does just that. Git hook syntax is a completely new language on its own.
Honorable mention: Take a webcam picture each time you commit
I came across this the other day and it’s hilarious. It was really the main reason for writing this article.
The name of the repo is already great, but the content is even better. As I was loading it, I thought to myself, “Please, please have sample images.” And it didn’t disappoint.
Takeaway: Git Hooks Can Be Amazingly Useful
I hope I could show you some practical examples in this article. I don’t use hooks that much, but even just the one that saves me from that certificate drama is a lifesaver and one of the reasons I still stay somewhat sane at my job.