- Published on
Improve Commit Message Quality with CommitLint
- Authors
- Name
- Hieu Cao
Introduction
Commit messages are an essential part of software development. They provide context for code changes, help team members understand the project history, and improve collaboration. However, writing consistent and meaningful commit messages can be challenging.
CommitLint is a tool designed to enforce conventional commit message formats. By integrating CommitLint into your workflow, you can maintain a clear and organized Git history.
In this blog, we'll explore the basics of CommitLint, its benefits, and how to set it up in your projects.
What Is CommitLint?
CommitLint is a tool that checks if your commit messages meet a specified standard. It works well with the Conventional Commits specification, which defines a consistent structure for commit messages, such as:
type(scope?): subject
Common Commit Types
- feat: A new feature
- fix: A bug fix
- docs: Documentation changes
- style: Code style updates (formatting, no functional changes)
- refactor: Code refactoring
- test: Adding or updating tests
- chore: Maintenance tasks (e.g., updating dependencies)
Benefits of Using CommitLint
- Consistency: Ensures all developers follow the same commit message format.
- Clarity: Makes it easier to understand the purpose of each commit.
- Automation: Reduces the need for manual code reviews for commit messages.
Setting Up CommitLint
Follow these steps to integrate CommitLint into your project.
Step 1: Install CommitLint
First, install CommitLint and the conventional commit config:
npm install @commitlint/cli @commitlint/config-conventional --save-dev
Step 2: Configure CommitLint
Create a commitlint.config.js
or commitlint.config.cjs
file in the root of your project:
module.exports = {
extends: ['@commitlint/config-conventional'],
}
Step 3: Add a Commit Hook with Husky
To enforce commit message validation, you can use Husky to run CommitLint as a pre-commit hook. Please follow this artice to setup Husky
& Lint-Staged
.
Create a commit-msg
file in the .husky
folder, then add content to the file as below:
npx --no-install commitlint --edit $1
Step 4: Test the Setup
Try committing a change with an invalid message:
git commit -m "invalid message"
CommitLint should display an error, preventing the commit.
Conclusion
CommitLint is a powerful tool for maintaining high-quality commit messages and a clean Git history. By enforcing conventional commits, you can enhance collaboration, improve code reviews, and make project management more efficient.
Start using CommitLint today to level up your Git workflow!
Happy committing!