TestVagrant

How can we improve our Git Commits using Husky?

How to improve Git commits using Husky

Blog

How Can We improve our Git Commits using Husky?

In a team, many mates use a single repository. Each has its way to write the code, putting commit messages, etc. But a general requirement always comes, i.e. Let’s follow some standard practices on code quality and commits.

Use Cases :

I am listing down a few real-time cases to understand the outcome of this blog better. 

  1. Before committing to GIT, code quality should be followed, which may be defined with external utils. e.g. eslint
  2. Commit messages should have proper syntax. We don’t prefer any random message on commits.
  3. Run the Unit tests, and the pass percentage should be 100% before pushing new changes to git.
  4. Run the sanity suite before pushing the changes.

Currently, I can think of two approaches –

One way can communicate process rules to all teammates and ask them to follow those standards. But it has its main drawback: sometimes we miss following intentionally or unintentionally.

And Another is, With Automation/Coding.

What if we can allow pushing into GIT only when defined standards are followed, that too with automation?

Yes, We can achieve this via the Husky package. Husky provides integration with basic GIT hooks.

Pre-Requisite:

  1. You should set up your Node package.
  2. You have integrated support libraries to check code quality, coverage, etc.

Installation and Setup :

Step 1: Run the command — npm install husky --save-dev

Step 2: Run the command — npx husky install

Note: Above steps will install the package and enable the git hooks in your system level for the respective repository. If you are taking a fresh clone of the repository on which husky is implemented, Don’t forget to enable the git hooks by running the command — npx husky install

Now we will create hooks, and we will add our rules.

Step 3: For demo purposes, I am creating a pre-commit hook. In the Pre-commit stage, we want to first check code standards as per eslint and run the unit tests. I am assuming to run the unit tests in your current project, the command is — npm test

To add the Pre-Commit hook, Run the command — npx husky add .husky/pre-commit

The above step will create a folder with the name — .husky in your project root directory.

Now we will add our rules in the GIT Pre-commit hook. Our .husky/pre-commit file will look like the below —

				
					#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx eslint . --ext .ts --fix. #Checking my code quality based on rules of eslint
npm test # Running Unit Test
				
			

Step 4: You can set your rules for other git hooks in the same way. For example, you want a proper message format in commit messages to integrate commitlint with .husky/commit-msg.

				
					#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx commitlint --edit "" # Check for commit message format via commitlint

				
			

Utilisation:

Till now, we completed the setup. Let’s now understand How we can utilize this implementation ?. It’s pretty simple. Write your code, and Now add your changes to git.

Did you notice anything..? Your eslint checks were done, and your unit test has started. (Based on pre-commit hook).

If Your code is qualifying the above checksit is Nice !! You will be able to push the changes.

But, if any of the above checks doesn’t passYou will not push the code until you fix all the faults.

Share This Article

Other Related Articles

Scroll to Top