Sub topic 9
Enforcing the format with a commit-msg hook and commitlint
This chapter assumes you can make commits and read git log. You should also know that a repository has a .git/hooks directory and that you can run commands in a terminal. A convention that relies on memory will drift. Enforcement catches a bad message at the moment it is written, when fixing it costs a few seconds.
Where enforcement can happen
A check can run in three places. Each has a different job.
| Place | When it runs | Strength | Weakness |
|---|---|---|---|
| `commit-msg` hook | Locally, as you commit | Instant feedback | Each developer must install it, and `--no-verify` skips it |
| commitlint in CI | On every push or pull request | Cannot be skipped by one developer | Feedback comes later |
| PR title check | On the pull request | Fits squash-merge workflows | Checks only one message |
A sensible setup uses a local hook for speed and CI as the real gate.
A hand-written commit-msg hook
Git runs the commit-msg hook after you write the message and before the commit is created. It passes the path of a temporary file holding the message as the first argument, $1. If the script exits with a non-zero status, Git aborts the commit.
#!/bin/sh
# .git/hooks/commit-msg (must be executable)
pattern='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-z0-9._-]+\))?!?: .+'
if ! head -n 1 "$1" | grep -Eq "$pattern"; then
echo "Commit message must look like: type(scope)!: description" >&2
exit 1
fiThe pattern reads left to right:
- One of the allowed types.
- An optional scope in parentheses.
- An optional
!for a breaking change. - A colon, a space, and at least one character of description.
Save it as .git/hooks/commit-msg, then run chmod +x .git/hooks/commit-msg. Without the executable bit, Git silently ignores the hook.
This hook only checks the first line. It does not verify the blank line before the body or the footer format. It also accepts any scope that matches the character set. For stricter rules, use a linter.
Linting with commitlint
commitlint parses the whole message and checks it against a set of rules. The @commitlint/config-conventional package supplies a ready ruleset based on the specification.
npm install --save-dev @commitlint/cli @commitlint/config-conventional
# commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] };
# check the most recent commit message
npx commitlint --from HEAD~1 --to HEADThe second line is the content of a config file named commitlint.config.js, not a shell command. Create that file in the project root.
You can also test a message without making a commit by piping it in:
echo "added stuff" | npx commitlintThis prints the failed rules, for example that the type is missing and the subject may not be empty. The exit code is non-zero, which is what lets hooks and CI treat it as a failure.
Running commitlint from a hook
Make .git/hooks/commit-msg call commitlint instead of a regular expression. Git passes the message file path, and commitlint can read it with --edit:
#!/bin/sh
npx --no -- commitlint --edit "$1"The --no flag stops npx from downloading packages on the fly, so the hook uses the version your project pinned.
Running commitlint in CI
Use the same config in CI so local and remote checks agree. To check every commit in a pull request, compare the branch against its base:
npx commitlint --from origin/main --to HEADIf your team squash-merges, only the pull request title ends up in history. In that case lint the title instead, covered in the next chapter.
Common errors
- Hook never runs. The file is not executable, is named
commit-msg.sh, or sits in a different hooks path thancore.hooksPathpoints to. - Everything passes locally but fails in CI. CI has an older config or a shallow clone, so
origin/mainis missing. Fetch full history first. - Merge and revert commits fail. commitlint ignores default Git-generated merge messages. Custom merge messages may not be ignored.
- Developers bypass the hook.
git commit --no-verifyskips local hooks. This is why CI is the real gate.
Exercise
A teammate says the commit-msg hook is installed and the message "update readme" still got committed. Name two likely causes and one way to make the rule hold regardless.
Show answer
Likely causes: the hook file is not executable, or the commit was made with --no-verify. The hook may also live only in another clone's .git/hooks. To make the rule hold, run commitlint in CI so a non-conforming message fails the pull request check.