Automatically Prevent Bad Git Commits

Some bad Git commits you can’t prevent. I’m talking about the ugly, last-minute implementations that pass the test suite but will give you nightmares for days. Or commits with nasty bugs that will only reveal themselves days later on a corner case.

Some bad commits, though, you absolutely can prevent. And there’s a way to automatically prevent them, every time you invokegit commit:Git Hooks

Git hooks come in two main varieties, client-side and server-side. I’ll only be referring to the client-side hooks here. Basically, a Git hook is a script that is run at any one of several points in the Git workflow. The hook we’ll be examining here is the “pre-commit” hook that fires off just after you executegit commit. It runs before you’re asked for a commit message or even given the default message.

Bob Gilmore has anexcellent collectionof these pre-commit hooks. You clone the repo in your home directory, then runsetup.shfollowed by the path to the Git repo you’d like to install the hooks into. Once that’s done, every time you commit in that repo, Bob’s script will check every committed file for: