Guide: Local setup
Get high commit message quality and short feedback cycles by linting commit messages right when they are authored.
This guide demonstrates how to achieve this via git hooks.
Follow theGetting Started for basic installation and configuration instructions.
Add hook
To use commitlint you need to setupcommit-msg hook (currentlypre-commit hook is not supported)
Using a git hooks manager
To lint commits before they are created you can useHusky'scommit-msg hook.
You can find complete setup instructions on theofficial documentation.
NOTE
The following instructions are meant tohusky@v9 if you are using a different version consult the official documentation of your version.
WARNING
For Windows users: ensure allhusky files areUTF-8 enconded. If any other format is used an error may be thrown at runtime such ascannot execute binary file.
npm install --save-dev husky# husky@v9npx husky init# husky@v8 or lowernpx husky install# Add commit message linting to commit-msg hookecho "npx --no -- commitlint --edit\$1" > .husky/commit-msg# Windows users should use ` to escape dollar signsecho "npx --no -- commitlint --edit `$1`" > .husky/commit-msgAs an alternative you can create a script insidepackage.json
npm pkg set scripts.commitlint="commitlint --edit"echo "npm run commitlint\${1}" > .husky/commit-msgUsing git hooks
Info about git hooks can be found onGit documentation.
WARNING
It's necessary that you usecommit-msg as the name for hook file.
Test
Test simple usage
For a first simple usage test of commitlint you can do the following:
npx commitlint --from HEAD~1 --to HEAD --verboseyarn commitlint --from HEAD~1 --to HEAD --verbosepnpm commitlint --from HEAD~1 --to HEAD --verbosebun commitlint --from HEAD~1 --to HEAD --verbosedeno task --eval commitlint --from HEAD~1 --to HEAD --verboseThis will check your last commit and return an error if invalid or a positive output if valid.
Test the hook
You can test the hook by simply committing. You should see something like this if everything works.
git commit -m "foo: this will fail"# husky > commit-msgNo staged files match any of provided globs.⧗ input: foo: this will fail✖ type must be one of [build,chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]✖ found 1 problems, 0 warningsⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlinthusky - commit-msg script failed (code1)Sincev8.0.0commitlint won't output anything if there are no problems with your commit.
(You can use the--verbose flag to get positive output)
git commit -m "chore: lint on commitmsg"# husky > pre-commitNo staged files match any of provided globs.# husky > commit-msgLocal linting is fine for fast feedback but can easily be tinkered with. To ensure all commits are linted you'll want to check commits on an automated CI Server too. Learn how to in theCI Setup guide.