Tomasz TJ Janczewski

Enterprise Architect / Security Architect

Single post

Bitbucket pre commit hooks with Husky for better code quality

BitBucket is Git Management solution from Atlasian. It is free for small teams up to 5 active team members and support all Git repository features. More about Bitbucket could be fount at their website .

Pre-commit hooks are powerful functionality to execute own scripts after staging git changes but before commit them to the local repository. Thanks to this solution developers are able to execute code quality checks and code tests before the code officially lands at the repository. Pre-commit hook is not the only hook supported by Git. Other common hooks are:

  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-rebase
  • post-checkout
  • post-merge
  • pre-receive
  • update
  • post-receive
  • post-update
  • pre-auto-gc
  • post-rewrite
  • pre-push

But the most important from code quality and test perspective are pre-commit and pre-push ones.

Husky is node js tool written by Typicode and supporters as open source support for bad git commit prevention. Project homepage is and of course

Putting all together. The goal is to perform code quality checks and test before committing them to local code repository to ensure best possible code stability.

First step like always for npm libraries and tools is installation.

npm install husky –save-dev or npm i husky

depends of Your need.

Next step is adding configuration to package.json

// package.json


„husky”: {

      „hooks”: {

         „pre-commit”: „npm test”,

         „pre-push”: „npm test”



General approach is “name of the hook” : “script / command to be executed”.

And that’s all!

You have full support to pre-commit hook and can execute any bash script like npm test.

Of course husky is not the only tool that I’m using but this article focus only on husky. Typically for node js (and general JavaScript) development it is worth to add eslint and Prettier for checking the code and auto formatting tools. Husky supports out of the box git event system – hooks and do not force developer to manually create them in Git folder structure. It is awesome supporting tool which keeps all needed configuration is standard package.json file which is always first step to investigate what node js application does.