Git Pre-Commit Hooks
The Git vs Mercurial debate has been raging in OnePageCRM since I started here this time last year. As the team has grown, the Git evangelists have outnumbered the Mercurial refuseniks, and the team finally made the transition earlier this month.
The main reasons we decided to move were the amount of modern tools and services that easily integrate with Git, and Git Flow - a branching strategy that really suits how our team is working.
In the early days of the changeover, a familiar problem popped up - developers accidentally committing debug information to the repository. We had become lazy and complacent, relying on our Mercurial pre-commit hooks to warn us when we tried to commit a
So, an update to our internal instructions was required.
By default, every Git repository has a
.git/hooks folder which contains samples for the different kind of hooks.
To create a pre-commit hook, you need write a script, name the file
pre-commit and make it executable.
I copied our new pre-commit script from Bartłomiej Danek on Coderwall (Thanks Bartłomiej!). I edited it slightly to add some extra keywords that we want to exclude. Here’s the final script we are using:
When I try to run
git commit with some dirty code, I get the following error:
All I need to do now is to ask the rest of the development team to add this file to their .git/hooks directory.
I was surprised at how easy and useful this feature is. The temptation to add other hooks enforcing coding style is strong, but I’ll hold myself back for now!