Prevent bad git commits and pushes with Husky

Share:
Source Code

When you’re working in a team environment, you always want to do your best to make sure your code is clean and error free before you push to a source control, but how often do you pull latest changes from your team and saw unclean, and error prone code? Wouldn’t it be nice to have something in place to force people to fix their code before they can even push it? Well good news, Husky is here to help.

What is Husky?

Url: https://www.npmjs.com/package/husky

Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff…)

 

Set up

Let’s get started by setting up a package.json file:

{
  "name": "Prehooks",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "prepush": "jshint index.js"
  },
  "devDependencies": {
    "husky": "0.7.0",
    "jshint": "^2.8.0"
  },
  "author": "PentaCode",
  "license": "ISC"
}

We’ll be using JSHint as our linter to run before we push our code, this is done via the scripts section:

  "scripts": {
    "prepush": "jshint index.js"
  },

this tells Husky to run jshint index.js when the user types git push.

Next we set up a .jshintrc file with basic linting rules:

{
    "curly": true,
    "eqeqeq": true,
    "latedef": false,
    "evil": true,
    "noarg": true,
    "undef": true,
    "eqnull": true,
    "node": true
}

Then we’ll create an index.js file that violates some of these rules:

var myFunction = function() {

    if (num == 10) {
        console.log('The number is 10')
    }

    while (num !== 10)
        doSomething()

    function doSomething() {
        console.log('do something')
    }

}

now let’s add the following line to this file:

console.log('hello world');  

and commit it to git:

git commit -m 'added hello world';  

then push it to your repository:

git push  

and if you done everything correctly, you should see something like the following:

husky git commit jshint error

Isn’t that amazing? No more bad code, no more buggy code and no more missing semicolons!

What if I want it to run pre-commit?

You got it, just change the scripts section to:

  "scripts": {
    "precommit": "jshint index.js"
  },

and husky and JShint will check for code quality before every commit!

Let’s be a good citizen by fixing our code:

var myFunction = function() {  
    var num;
    if (num === 10) {
        console.log('The number is 10');
    }

    while (num !== 10) {
        doSomething();
    }

    function doSomething() {
        console.log('do something');
    }

    console.log('hello world');
};

and then push it:

git commit -m 'Fixed jshint errors'  
git push  

Viola!

What if I want to skip it?

Shame on you! You should always run it, but for the rare cases where you want to skip it… you can add –no-verify to bypass it, so the full command would be:

git push --no-verify  

or

git commit -m 'SHAME ON ME' --no-verify  

Possibilities are endless

You can use it for tons of other things, for instance, you can let husky run your tests before every commit or push, or even clean up directories or run some grunt or gulp tasks… the possibilities are endless!

Comments Or Questions? Discuss In Our Discord

If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!

More from PentaCode