Newer Post

Scaling your tests with an army of clones

Older Post

Lightning Talk: Be a GIVER

Build a Fearless Development Cycle With Git Hooks

One of the greatest things about programming is that you can change stuff. You can modify them, shape, bend to your needs. But as good as it may sound, unfortunately it is a double-edged sword. Having the ability to change is tempting and dangerous at the same time.

If you work in a development team, especially one made of mostly remote developers, you might find that without necessary preparations, you will spend plenty of time on obvious bugs fixes, coding guidelines changes (if you have one!) or linting errors patches.

That is one of the reasons why you have to be adequately prepared and automate tasks, which should be performed before someone sends any code change for a review. Even worse, when those changes go straight to the codebase without even realizing they are not written in a correct manner. You have to remember that adding an additional manual step to a development process is not the greatest idea, as people are just not perfect and they may sometimes simply forget one of the steps.

Fortunately, if you use Git, you have a great solution right before you. Git Hooks are just small scripts that are run in a specific time, based on what command you are trying to use at the given moment.

Right now there are 16 available hooks and some of them are pre-commit, post-commit, pre-receive or pre-rebase. For a full list you can visit git hooks documentation.

Every git repository has its own hooks in a form of separate files named after hook’s type, which can be found in .git/hooks folder. You can place any script there – bash, node, ruby, as long as it will return valid exit codes, 0 for success or >= 1 for an error.

The hook that we will use here is called pre-commit and is triggered right before our commit gets created. This will help us to warn and prevent the developer from committing broken or wrongly formatted code.

There are two common ways of doing that. You can use either configure hooks on your own, or if you are using npm, you can make use of precommit-hook module that will set up hook scripts for you, based on your package.json configuration.

Just to keep it simple, we will use only 3 tools as our pre-commit validation, jshint for linting process, jscs for coding style checks and tape-run as our unit tests runner, with addition of jshint-stylish and tap-spec as our reporters, so everything looks nice and readable. And we will also assume that we are using npm here, but if you don’t want to use it, feel free to write your git hook by hand, it will work just fine as well.

Let’s start by installing precommit-hook package and storing it in our package.json file as devDependency.

We'll help you unleash.

Join the 30,000 developers who subscribe to our newsletter.

Scale your
Development team

We help you execute projects by providing trusted developers who can join your team and immediately start delivering high-quality code.

Hire Developers
code