Enforcing Consistent Commit Messages with CommitLint
CommitLint is a small package that lints Git commit messages against a style guide you define. Commit messages tend to get sloppy over the life of a project. CommitLint catches that by rejecting anything that doesn't match the convention.
Why use CommitLint
A few concrete reasons to add it to a workflow:
- A consistent style keeps commit history predictable across a team, and makes generating changelogs or release notes far less manual.
- Structured messages (a JIRA ticket number, a clear type prefix) are easier for someone unfamiliar with the change to parse later, including future you.
- It also catches basic mistakes: inappropriate language, or a message written in the wrong language for the project.
How to set it up
Install it with npm from the root of your project:
npm install commitlint --save-dev
Then generate a config file, which is where the actual rules live:
npx commitlint --init
That creates commitlint.config.js. Edit it to change which rules CommitLint enforces.
To run CommitLint automatically on every commit rather than by hand, install Husky (opens in a new tab) first:
npm install husky --save-dev
Activate Husky's hooks:
npx husky install
Then wire CommitLint into the commit-msg hook:
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit ${1}'
The local setup guide (opens in a new tab) covers this in more depth if any of these steps behave unexpectedly.
Configuration options
commitlint.config.js supports a wide range of options. The ones you'll actually touch:
rules: an object where each key is a specific rule, set to on or off.parserPreset: which parser CommitLint uses to read commit messages.formatter: how CommitLint's output gets displayed.
The full reference (opens in a new tab) covers everything else.
Running it in CI
Add CommitLint as a step in your pipeline so a message that slipped past a local hook still gets caught. In Travis CI, for example:
script:
- npm run lint:commit
That runs CommitLint against every commit in the build, so nothing merges without a message that follows the convention.
Conclusion
CommitLint doesn't do anything glamorous. It just enforces a style guide consistently, which is exactly the kind of thing humans are bad at doing by hand over a long project. The payoff is a commit history that's actually useful to the next person who has to read it.