Standardized Commit Messages with Conventional Commits
Set up and enforce the Conventional Commits specification with commitlint and commitizen for consistent, parseable commit messages.
Prerequisites
- -Node.js and npm installed
- -A Git repository
- -Basic understanding of commit message conventions
Steps
Install commitlint and the conventional config
Install commitlint, which validates commit messages, along with the conventional commits preset that defines the rules.
Create the commitlint configuration file
Create a config file that tells commitlint to use the conventional commits ruleset. Types include feat, fix, docs, style, refactor, test, chore, and more.
You can customize rules by adding a 'rules' object to the config. For example, set max header length or allow custom types.
Install husky for Git hooks
Install husky and initialize it. This creates a .husky directory with a pre-commit hook template.
Add a commit-msg hook for commitlint
Create a commit-msg hook that runs commitlint against every commit message. Invalid messages will be rejected.
Make sure the hook file is executable. On Unix systems, run 'chmod +x .husky/commit-msg'.
Test with a valid conventional commit
Create a test commit using the conventional format: type(optional-scope): description. The hook should pass.
Common types: feat (new feature), fix (bug fix), docs (documentation), refactor (code restructuring), test (adding tests), chore (maintenance).
Test with an invalid commit message
Try committing with a non-conventional message. Commitlint should reject it with an error explaining the required format.
Full Script
FAQ
Discussion
Loading comments...