Linting Git Commit Messages Using Husky: A Guide to Cleaner Version Control

Sijo Joseph
3 min readJul 14, 2023

Git commit messages are the breadcrumbs of your project’s history, helping you understand the purpose and context of each code change. However, inconsistent or poorly formatted commit messages can make this task challenging. That’s where linting git commit messages comes in.

In this guide, we’ll explore how linting can ensure your commit messages are clear, informative, and follow a consistent format. By adopting commit message linting, you can enhance collaboration, streamline code review processes, and improve the overall maintainability of your project.

Let’s dive in and discover how linting git commit messages can transform your version control experience.

Streamline Your Git Workflow with Husky

Managing a Git repository involves more than just writing code. It’s important to ensure that certain tasks, such as running tests, formatting code, or validating commit messages, are executed consistently and efficiently. This is where Husky comes into play.

Husky is a popular Git hook manager that simplifies the process of setting up and managing Git hooks in your projects. With Husky, you can easily configure pre-commit and pre-push hooks that run custom scripts or commands, allowing you to enforce code quality standards and automate essential tasks.

Prerequesties

Since we will be using npm packages you need to have Node and npm installed. Also we need to initialize our project with Git

Lets Start!

Validating Commit Messages with Husky

First, we need to install the latest version of Husky using the following command:

npm install husky -D

And then we need to enable Git Hook by below command:

npx husky-init

It will:

  1. Add prepare script to package.json
  2. Create a sample pre-commit hook that you can edit (by default, npm test will run when you commit)
  3. Configure Git hooks path
./husky/pre-commit

We can remove the npm test command if it’s not needed. Additionally, we can configure other hooks like prettier format or eslint if desired.

If you want to test a hook, you can add exit 1 at the end of the script to abort git command.

# ...
exit 1 # Commit will be aborted

The next step would be creating a file in the Husky directory for linting commit messages:

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit'

This will create a file called commit-msg in .husky directory with the following information:

.husky/commit-msg

Now we need to install commitlint and commitlint/clitpackages using npm command:

npm install @commitlint/config-conventional @commitlint/cli -D

Now we want to add our rules for commit messages and create a file called .commitlintrc.json and paste the following code into it:

.commitlintrc.json

commitlint checks if your commit messages meet the conventional commit format.

In general the pattern mostly looks like this:

type(scope?): subject  #scope is optional; multiple scopes are supported (current delimiter options: "/", "\" and ",")

The first word should be one of the rules items you have written in your .commitlintrc.json file and the scope is the module/component you are working on.

Congratulations! You’re all set to go.

If you make an improper commit message when committing your Git changes, you will receive an error message similar to the following.

git commit -m "invalid commit message"
Blocking Invalid Commit Messages with Husky’s Validation

if your commit messages meet the conventional commit format. it will sccessfully commit the changes.

git commit -m "fix: a valid commit message"
With conventional commit message format

Feel free to share your thoughts and ideas in comments.

Happy coding!

--

--