patternjavascriptTip
commitlint: enforcing conventional commit messages
Viewed 0 times
@commitlint/cli 19+
commitlintconventional commitscommit-msg hooksemantic releasechangelog automation
Error Messages
Problem
Commit messages in the repository are inconsistent ('fix stuff', 'wip', 'asdf'), making changelogs impossible to generate automatically and history hard to navigate.
Solution
Install commitlint with the conventional commits config and wire it to the commit-msg git hook via husky.
# Install
npm install -D @commitlint/cli @commitlint/config-conventional
# commitlint.config.ts
export default { extends: ['@commitlint/config-conventional'] };
# .husky/commit-msg
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no -- commitlint --edit $1
# Valid commit formats:
# feat(auth): add OAuth2 login flow
# fix(api): handle null response from /users endpoint
# chore(deps): bump typescript from 5.3 to 5.4
# docs: update contributing guide
# BREAKING CHANGE: bump in the footer or feat! for breaking
# Install
npm install -D @commitlint/cli @commitlint/config-conventional
# commitlint.config.ts
export default { extends: ['@commitlint/config-conventional'] };
# .husky/commit-msg
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no -- commitlint --edit $1
# Valid commit formats:
# feat(auth): add OAuth2 login flow
# fix(api): handle null response from /users endpoint
# chore(deps): bump typescript from 5.3 to 5.4
# docs: update contributing guide
# BREAKING CHANGE: bump in the footer or feat! for breaking
Why
Conventional Commits is a specification that pairs with tools like Changesets, semantic-release, and standard-version. When every commit follows the format, changelogs and version bumps can be computed automatically from the commit history.
Gotchas
- commitlint runs on the commit-msg hook, which receives the path to the temporary commit message file as $1
- Merge commits and revert commits are allowed by default in @commitlint/config-conventional
- The scope (in parentheses) is optional but encouraged for large monorepos
- Use 'commitlint --from=HEAD~10' to lint recent commits retroactively during a migration
Context
Enforcing commit message standards for automated changelog generation or team consistency
Revisions (0)
No revisions yet.