Incentives ?

The reason someone might want commit message linting is to standardise commit messages, much like linting TypeScript or JavaScript helps improve readability and consistency in code. By applying clear rules, project’s commit message can easier to understand, review, and maintain. And more importantly, when you browser through the git history, it is easier to track changes and identify the purpose of each update.

Below are two examples of commit history BEFORE and AFTER using commit linting:

2026-07-19T084945

BEFORE linting (RED):

  • Inconsistent formatting: no enforced pattern in commit message, combined upper-case/lower-case letter for body
  • Hard to browse: You cannot easily browser this history and find out the commit relevant to you

AFTER linting (GREEN):

  • Consistent formatting: enforced pattern type(scope?): subject body?, enforce letter casing
  • Easy to browse: you can quickly look through the list and see what kind of work was done, like feat for new features, fix for bug fixes, docs for documentation, and chore for maintenance.
  • (Bonus) Automated Changelogs: Because the commit messages are easy for machines to read, tools like standard-version or semantic-release can look at the history, work out the next version number (for example, feat means a minor bump and fix means a patch bump), and then automatically create a neat CHANGELOG.md file.

Solution ?

For local setup commitlint with husky (git hook ran on devleoper’s computer)

conventional-changelog/commitlint:

Just as tools like ESLint or Prettier catch formatting errors in your code, commitlint is a tool that catches formatting errors in your Git commit messages. It acts as a gatekeeper, ensuring that every commit added to a project adheres to a specific, agreed-upon format before it is permanently written to the Git history.

By default, commitlint enforces the Conventional Commits specification, a lightweight convention built on top of commit messages that makes them readable for both humans and machines.

typicode/husky:

Husky is a popular developer tool that makes Git hooks easy to implement, manage, and share across a team.

Git inherently supports “hooks”, which is scripts that run automatically before or after Git actions like commit and push. (I have a post earlier about this if you are interested). However, native Git hooks are hidden locally in a developer’s .git folder and are not shared when teammates clone the repository. Husky solves this by bringing those hooks directly into your project’s source code, ensuring everyone on the team uses them automatically.

(Husky itself doesn’t have the ability to link commit message, all it does is using git hook to trigger commitlint command)

We will only cover the local setup in the upcoming portion of this post, but there are also other ways to apply commit linting using commitlint. You can read more about this in the following links:

But overall, I still believe linting in the local setup is most bulletproof, as it prevents you from pushing to the remote completely when the commit message is ill-shaped.


Step-by-Step ?

(you can read more about this at: https://commitlint.js.org/guides/local-setup.html)

Step-1: install husky

1
2
3
npm install --save-dev husky
npx husky init 
rm .husky/pre-commit

(*the init command will generate the .husky directory with a pre-populated example hook running npm test, of which you can safely delete via rm .husky/pre-commit)

Step-2: install commitlint

1
2
npm install --save-dev @commitlint/cli @commitlint/config-conventional
echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

Note that: the second command generates a default commitlint configuration using conventional config with default settings, but you can populate your own commitlint.config.js configuration file; you can read more about how to setup your own configuration file in the official documentation: https://commitlint.js.org/reference/configuration.html

For instance, below is an example of the custom configuration file found in a dev.to blog post:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// commitlint.config.mjs
const config = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    // 1. Format: <type>([optional scope]): <description> - enforced by most rules below.
    // Enforce that the type is not empty.
    'type-empty': [2, 'never'],

    // Enforce specific commit types. Add/remove types based on the project.
    'type-enum': [
      2, 
      'always', 
      ['feat', 'fix', 'build', 'chore', 'ci', 'style', 'refactor']
    ],

    // Subject/Description Rules:
    // 2. Short and Summarized: Try to fit the subject line inside 50 characters.
    'header-max-length': [2, 'always', 50],

    // 3. Capitalize the description: Start subject line with a capital letter.
    // Imperative Mood. Mostly useful for generating changelogs.
    'subject-case': [2, 'always', 'sentence-case'],

    // 4. Avoid trailing period.
    'subject-full-stop': [2, 'never', '.'],

    // Body Rules:
    // 5. Body is added by leaving a blank line after the subject line.
    'body-leading-blank': [2, 'always'],

    // 6. Wrap the body at 72 characters.
    'body-max-line-length': [2, 'always', 72],

    // Footer Rules:    
    // Ensure a blank line precedes the footer.
    'footer-leading-blank': [2, 'always'],

    // Rule for Optional Scope:
    // Enforce that if a scope is used, it is in lower-case.
    'scope-case': [2, 'always', 'lower-case'],
  } 
};
export default config;

Step-3: setup git hook

Then let’s use husky to trigger execution of commitlint using git hook whenever a Git commit command is being run:

1
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg

Step-4: test committing

Finally, let’s test using two different commit messages. One using well-formatted commit message string that aligns with the Conventional format, One using ill-formatted commit message that does not:

2026-07-19T121747


Conventional Commits (Specification)

As shown earlier (and also recommended in the official documentation), the commitlint uses Conventional Commits as its default specification for commit message: https://www.conventionalcommits.org/en/v1.0.0/.

Summary of this specification is as follows:

The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.

And the details of this classification are below:

The commit message should be structured as follows:

1
2
3
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]

The commit contains the following structural elements, to communicate intent to the consumers of your library:

  1. fix: a commit of the type fix patches a bug in your codebase (this correlates with PATCH in Semantic Versioning).
  2. feat: a commit of the type feat introduces a new feature to the codebase (this correlates with MINOR in Semantic Versioning).
  3. BREAKING CHANGE: a commit that has a footer BREAKING CHANGE:, or appends a ! after the type/scope, introduces a breaking API change (correlating with MAJOR in Semantic Versioning). A BREAKING CHANGE can be part of commits of any type.
  4. types other than fix: and feat: are allowed, for example @commitlint/config-conventional (based on the Angular convention) recommends build:, chore:, ci:, docs:, style:, refactor:, perf:, test:, and others.
  5. footers other than BREAKING CHANGE: <description> may be provided and follow a convention similar to git trailer format.

Additional types are not mandated by the Conventional Commits specification, and have no implicit effect in Semantic Versioning (unless they include a BREAKING CHANGE). A scope may be provided to a commit’s type, to provide additional contextual information and is contained within parenthesis, e.g., feat(parser): add ability to parse arrays.

Then here are some examples provided in the official documentation:

Commit message with description and breaking change footer

1
2
3
feat: allow provided config object to extend other configs

BREAKING CHANGE: `extends` key in config file is now used for extending other config files

Commit message with ! to draw attention to breaking change

1
feat!: send an email to the customer when a product is shipped

Commit message with scope and ! to draw attention to breaking change

1
feat(api)!: send an email to the customer when a product is shipped

Commit message with both ! and BREAKING CHANGE footer

1
2
3
feat!: drop support for Node 6

BREAKING CHANGE: use JavaScript features not available in Node 6.

Commit message with no body

1
docs: correct spelling of CHANGELOG

Commit message with scope

1
feat(lang): add Polish language

Commit message with multi-paragraph body and multiple footers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fix: prevent racing of requests

Introduce a request id and a reference to latest request. Dismiss
incoming responses other than from latest request.

Remove timeouts which were used to mitigate the racing issue but are
obsolete now.

Reviewed-by: Z
Refs: #123

Reference