How to lint your commit messages


First install husky, which sets up your Git hooks:

$ npm install husky

Then install commitlint with a config, which will be used to lint your commit message:

$ npm install @commitlint/{cli,config-conventional}

Note: The command above installs both @commitlint/cli and @commitlint/config-conventional.

Since we’re using config-conventional, it means we’re following a standard based on the Angular commit convention.

Create .huskyrc, which runs commitlint during the Git commit hook:

{
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
}

Or add the following to package.json (it accomplishes the same thing as above):

{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "devDependencies": {
    "@commitlint/cli": "latest",
    "@commitlint/config-conventional": "latest",
    "husky": "latest"
  }
}

Now create .commitlintrc.json, which extends the rules from config-conventional:

{
  "extends": ["@commitlint/config-conventional"]
}

Or export the rules in commitlint.config.js:

module.exports = {
  extends: ['@commitlint/config-conventional'],
};

You can now commit and verify that the message is following the Conventional Commits format:

$ git commit -m "add commitlint feature" # fail
$ git commit -m "feat: add commitlint"   # success


If you enjoyed this post, please consider supporting this site!