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:

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
featfor new features,fixfor bug fixes,docsfor documentation, andchorefor 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
.gitfolder 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:
- CI Setup (using GitHub Actions / GitLab CLI, etc): https://commitlint.js.org/guides/ci-setup.html
- AI Agent (using skills.md): https://commitlint.js.org/guides/ai-agents.html
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
| |
(*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
| |
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:
| |
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:
| |
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:

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:
- fix: a commit of the type
fixpatches a bug in your codebase (this correlates withPATCHin Semantic Versioning).- feat: a commit of the type
featintroduces a new feature to the codebase (this correlates withMINORin Semantic Versioning).- BREAKING CHANGE: a commit that has a footer
BREAKING CHANGE:, or appends a!after the type/scope, introduces a breaking API change (correlating withMAJORin Semantic Versioning). A BREAKING CHANGE can be part of commits of any type.- types other than
fix:andfeat:are allowed, for example @commitlint/config-conventional (based on the Angular convention) recommendsbuild:,chore:,ci:,docs:,style:,refactor:,perf:,test:, and others.- 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 3feat: allow provided config object to extend other configs BREAKING CHANGE: `extends` key in config file is now used for extending other config filesCommit message with
!to draw attention to breaking change
1feat!: send an email to the customer when a product is shippedCommit message with scope and
!to draw attention to breaking change
1feat(api)!: send an email to the customer when a product is shippedCommit message with both
!and BREAKING CHANGE footer
1 2 3feat!: drop support for Node 6 BREAKING CHANGE: use JavaScript features not available in Node 6.Commit message with no body
1docs: correct spelling of CHANGELOGCommit message with scope
1feat(lang): add Polish languageCommit message with multi-paragraph body and multiple footers
1 2 3 4 5 6 7 8 9 10fix: 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
- CommitLint: Lint commit messages
- Husky: Modern native Git hooks made easy
- Landing: https://typicode.github.io/husky/
- GitHub: https://github.com/typicode/husky
- Dev.to - shinjith - Git Good: Automating Commit Message Standards with Husky and Commitlint
- YouTube - Sikandar Dev - How to Setup Commitlint & Husky in Your Project