You can use commitlint with git pre-commit hook.
The simplest way I found to use it is via pre-commit framework.
The nice thing is that you can easily add and customize any other check before commiting, here's a list of supported hooks.
Adding commitlint
with pre-commit
pip install pre-commit
or brew install pre-commit
Create your .pre-commit-config.yaml
(check and update rev if needed):
repos:
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v2.3.0
hooks:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ['@commitlint/config-conventional']
args: ["--config",".commitlintrc.yaml"]
You have a lot of options with a commitlint configuration file .commitlintrc.yaml
:
rules:
body-leading-blank: [1, always]
body-max-line-length: [2, always, 100]
footer-leading-blank: [1, always]
footer-max-line-length: [2, always, 100]
header-max-length: [2, always, 100]
subject-case:
- 2
- never
- [sentence-case, start-case, pascal-case, upper-case]
subject-empty: [2, never]
subject-full-stop: [2, never, "."]
type-case: [2, always, lower-case]
type-empty: [2, never]
type-enum:
- 2
- always
- [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]
Install commit message hook
pre-commit install --hook-type commit-msg
And then you can try to git commit
.
You can also install commitlint directly via npm, here's how.
# Install commitlint cli and conventional config
npm install --save-dev @commitlint/{config-conventional,cli}
# Configure commitlint to use conventional config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
I recommend Conventional Commits format.
The commit message should be structured as follows:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Default types:
build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
Examples:
feat(parser): add ability to parse arrays
docs: correct spelling of CHANGELOG
ci: add docker build
Some benefits of using this structure:
- Automatically generating CHANGELOGs.
- Automatically determining a semantic version bump (based on the types of commits landed).
- Communicating the nature of changes to teammates, the public, and other stakeholders.