Git Hooks Made Easy – Husky

Git Hooks Made Easy - Husky

Git hooks are custom scripts that you can drop inside your .git directory which are executed at a specified point in time. These scripts are usually executed after a certain event is occurred or about to occur. For example, we can have a script i.e. executed before commit or after the commit has happened. These scripts can be used to perform automated tasks such as executing all the test cases before pushing the changes to the git or sending an email notification to all the members when a user pushes the changes to git.

The Problem!

The git hooks are located inside the .git/hooks directory. The problem with hooks is you cannot commit and push your hooks and share it with your team members since the .git folder is not versioned.

Husky to the rescue!

To install husky in your project all you need to do is open your terminal and type following npm command.

npm install --save-dev husky

You could also use yarn to install this package.

yarn add husky --dev

Now, to add new hooks to you’re project open your package.json file.

  "husky": {
    "hooks": {
      "pre-commit": "npm run lint",
      "pre-push": "npm test",
      "...": "..."

In the above example, we’re using to hooks first executes linting before committing the changes and second runs all your tests before pushing the changes to the repository. If any of the npm scripts fails the git action following the script won’t be executed. You are free to replace npm run lint or npm test with your own npm scrips.

You can also run more then one script in your hook using the &&. For example npm run lint && npm test.

For more Information:


Must Read: Angular Meta Service for Meta Tags

Leave a Reply

Your email address will not be published. Required fields are marked *