Validate commit message using Commitlint and husky

Validate commit message using Commitlint and husky

Commit message is very important when you work in team. By reading the conventional commit messages, it helps your team to understand what changes have you done and why. Sometime it’s also going to help you when you look back into your codebase 😜 . You will be using Commitlint for validating commit message.

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

Install commitlint:

There are few convention we can use:

  1. @commitlint/config-angular
  2. @commitlint/config-conventional
  3. @commitlint/config-lerna-scopes
  4. @commitlint/config-patternplate

To add config-conventional into project,

Install @commitlint/config-conventional:

Create commitlint.config.js in root of the project:

commitlint.config.js:

Configuration is picked up from commitlint.config.js or a commitlint field in package.json.

GitHooks with Husky 🐶

It’s not dog 😜. We’re talking about the tool that allows you to set up Git hooks very easily.

You can add Git hooks in two easy steps:

  • Install husky as a dev dependency:
  • Insert the following code in your package.json:
  • Add Hook

After running above command, you will able to see .husky in root of the project.

That’s all. At each commit, the command associated with commit-msg will be run. If you commit with wrong commit message, you will get below error as below:

Your final package.json will look like below snippet:

package.json

Note: 🧨

All Dependencies should be installed as development dependencies, you don’t need to add directly into dependencies.

Reference 🧐

🌟 Twitter | 👩🏻‍💻 suprabha.me | 🌟 Instagram

Frontend Developer sharing JS, HTML, CSS, GoLang🔥 Find all on blog.suprabha.me💛

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store