Linting Commits with commitlint
Conventional Commits only help if every commit follows them; one Fixed stuff on main and the release tool either ignores a fix or fails. commitlint checks messages against a set of rules, locally in a Git hook and again in CI, where nobody can skip it. The sessions use commitlint 21.2.4 with @commitlint/config-conventional.
Pass and fail
commitlint reads the message from standard input (or from a file, or from Git history) and the configuration from commitlint.config.mjs (also .commitlintrc.json, package.json and a few other names):
mkdir app && cd app && git init -q
cat > commitlint.config.mjs <<'CFG'
export default { extends: ['@commitlint/config-conventional'] };
CFG
echo "feat(api): add pagination" | commitlint && echo "accepted"
echo "Added pagination" | commitlint; echo "exit code: $?"
echo "feat: Add Pagination." | commitlint; echo "exit code: $?"
printf 'fix(db)!: drop the legacy column\n\nBREAKING CHANGE: column x was removed\n' | commitlint --verboseaccepted
⧗ --- input ---
Added pagination
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
exit code: 1
⧗ --- input ---
feat: Add Pagination.
✖ subject must not be sentence-case [subject-case]
✖ subject may not end with full stop [subject-full-stop]
✖ found 2 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
exit code: 1
⧗ --- input ---
fix(db)!: drop the legacy column
BREAKING CHANGE: column x was removed
✔ found 0 problems, 0 warnings
The exit code is what CI and Git hooks look at: 0 accepts, 1 rejects. Each problem names the rule that failed ([subject-case]), which is also the key to change it in the configuration. --verbose prints the report even for a passing message.
Rules and levels
Every rule is [level, applicable, value]:
| Part | Values |
|---|---|
| level | 0 off, 1 warning (reported, exit code stays 0), 2 error |
| applicable | 'always' or 'never' |
| value | rule-specific: a list, a number, a case name |
config-conventional sets, among others, type-enum (the allowed types: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test), subject-case (rejects sentence-case, start-case, pascal-case and upper-case subjects), subject-full-stop, header-max-length (100) and body-max-line-length (100). A project overrides or adds rules on top:
mkdir app && cd app && git init -q
cat > commitlint.config.mjs <<'CFG'
export default {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-enum': [2, 'always', ['api', 'cli', 'core', 'deps']],
'scope-empty': [1, 'never'],
'header-max-length': [2, 'always', 60],
'type-enum': [2, 'always', ['feat', 'fix', 'perf', 'docs', 'chore', 'ci', 'revert']],
},
};
CFG
check() { printf '%s\n' "$1" | commitlint >/tmp/out.txt; echo "$? <- $1"; grep -E '^(✖|⚠)' /tmp/out.txt | grep -v found; }
check "feat(cli): add --json flag"
check "fix(ui): align the buttons"
check "docs: explain the plugin API"
check "refactor(core): split the cache module"
check "feat(core): add a cache that keeps parsed configuration files in memory"0 <- feat(cli): add --json flag
1 <- fix(ui): align the buttons
✖ scope must be one of [api, cli, core, deps] [scope-enum]
0 <- docs: explain the plugin API
⚠ scope may not be empty [scope-empty]
1 <- refactor(core): split the cache module
✖ type must be one of [feat, fix, perf, docs, chore, ci, revert] [type-enum]
1 <- feat(core): add a cache that keeps parsed configuration files in memory
✖ header must not be longer than 60 characters, current length is 71 [header-max-length]
docs: explain the plugin API passes with a warning (exit code 0), because scope-empty is level 1. Warnings are useful for rules you are introducing gradually; switch them to errors once the team has adapted.
In a Git hook
A commit-msg hook runs before the commit is created and receives the path of the message file. commitlint's --edit option reads that file. Keep hooks in the repository (here .githooks/) and point Git at them; tools such as husky or lefthook do the same thing with less typing:
mkdir app && cd app && git init -q
echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.mjs
mkdir .githooks
cat > .githooks/commit-msg <<'HOOK'
#!/bin/sh
exec commitlint --edit "$1"
HOOK
chmod +x .githooks/commit-msg
git config core.hooksPath .githooks
git add -A
git commit -q -m "Initial commit" 2>&1 | grep -E '✖'
echo "commits so far: $(git rev-list --all --count)"
git commit -q -m "chore: initial commit" && git log --oneline✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
commits so far: 0
e876c00 chore: initial commit
Hooks are a convenience, not a control: git commit --no-verify skips them, and nothing installs core.hooksPath on a fresh clone. The check that counts runs in CI.
In CI: a range of commits
In CI there is no message file; commitlint reads the commits from Git. --from/--to lint everything after --from up to --to, and --last lints the most recent commit:
mkdir app && cd app && git init -q
echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.mjs
git add -A && git commit -q -m "chore: add commitlint"
base=$(git rev-parse HEAD)
git commit -q --allow-empty -m "feat(api): add pagination"
git commit -q --allow-empty -m "WIP"
git commit -q --allow-empty -m "fix(api): off-by-one in page size"
commitlint --from "$base" --to HEAD; echo "range exit code: $?"
commitlint --last && echo "last commit ok"⧗ --- input ---
WIP
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
range exit code: 1
last commit ok
In a GitHub Actions pull-request workflow the range is the PR's base and head. This is a non-run excerpt; the full workflow structure is in Workflows & Triggers:
# .github/workflows/commitlint.yml (excerpt)
on: pull_request
jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0 # the whole range must be present
- uses: actions/setup-node@v7
with:
node-version: 24
- run: npm ci
- run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verboseIf your repository squash-merges pull requests, lint the PR title instead (it becomes the commit on main): pass it on standard input, for example echo "$TITLE" | npx commitlint with TITLE: ${{ github.event.pull_request.title }} set as an environment variable (never interpolate untrusted text such as a PR title directly into a run: script; see Permissions, OIDC & Pinning by SHA).
Test yourself
A rule is configured as [1, 'never']. What happens when it fails?
- The commit is rejected
- A warning is printed, and the exit code stays 0
- Nothing at all
- commitlint crashes
Why isn't a commit-msg hook enough on its own?
- Hooks can't run Node
- Hooks can be skipped with
--no-verifyand aren't installed on fresh clones; CI must check too - Hooks only run on merge commits
- commitlint can't read files
Which commits does commitlint --from A --to HEAD check?
- A and every commit after it
- The commits after A up to and including HEAD
- Only HEAD
- Only A