Your Commits Refined with Commitizen and Commitlint
When it comes to maintaining a clean and consistent commit history, Commitizen and Commitlint emerge as powerful tools.
They not only guide developers in crafting meaningful commit messages but also ensure adherence to conventional commit standards. In this article, we’ll dive deep into these tools, how they work, and how to set them up in your projects.
What is Commitizen?
Commitizen is a CLI tool that prompts developers with questions during the commit process, ensuring that every commit message follows a standardized format.
By using Commitizen, you can create more readable and organized commit messages that can be easily interpreted by your team and tools like changelogs or release scripts.
What is Commitlint?
Commitlint enforces conventional commit rules by linting your commit messages. It validates your commit message against a set of rules, helping you maintain consistency across your commit history.
If your message doesn’t adhere to the predefined standards, Commitlint will flag it, prompting you to make corrections before proceeding.
Setting Up Commitizen and Commitlint
Step 1: Install Commitlint
First, you’ll need to install the necessary packages to enable Commitlint. Run the following command to add commitlint/config-conventional and commitlint/cli as development dependencies:
bash
yarn add @commitlint/config-conventional @commitlint/cli -D
Next, create a commitlint.config.js
file to configure Commitlint:
javascript
module.exports = {
extends: ["@commitlint/config-conventional"],
};
Step 2: Integrate Husky for Git Hooks
Husky allows you to create Git hooks, enabling automated checks before actions like committing or pushing code. To integrate Husky, initialize Git in your project (if not already done):
bash
git init
Then, install Husky and add the necessary hooks:
bash
npx husky-init && npm install
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
This setup ensures that Commitlint runs every time you make a commit, preventing any non-conventional commit messages from being added to your repository.
Step 3: Install Commitizen
Now, let’s set up Commitizen. Install it as a development dependency along with its adapter:
bash
yarn add --dev commitizen cz-conventional-changelog
Add the following configuration to your package.json
file:
json
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
You can then create a script to trigger Commitizen:
json
"scripts": {
"commit": "git-cz"
}
Step 4: Making a Commit with Commitizen
With everything set up, you can now use Commitizen to create a commit. Run the following commands:
bash
git add .
yarn commit
Commitizen will prompt you with a series of questions to ensure your commit message is formatted correctly:
- Select the type of change: Choose the category that best fits your change (e.g.,
feat
,fix
,docs
). - What is the scope of this change: Optionally specify the scope of the change.
- Write a brief description: Provide a short, imperative description of the change.
- Provide a longer description: Add more details if necessary.
- Are there any breaking changes?: Indicate if the change introduces any breaking changes.
- Does this change affect any open issues?: Link the commit to any relevant issues.
Once you’ve completed these prompts, your commit message will be generated and committed, ensuring it adheres to the conventional commit standards.
Why Use Conventional Commits?
Adopting conventional commits offers numerous benefits:
- Automated Changelog Generation: Automatically generate changelogs based on your commit history.
- Semantic Versioning: Determine version bumps (patch, minor, major) based on commit types.
- Improved Communication: Clearly communicate the nature of changes to team members and stakeholders.
- Streamlined Contribution Process: Make it easier for others to understand and contribute to your project by maintaining a structured commit history.
Conclusion
Commitizen and Commitlint provide a structured approach to managing commits, helping you maintain a clean and consistent commit history. By integrating these tools with Husky and possibly even standard-version, you can automate many aspects of your release process, making your development workflow more efficient and error-free. Whether you’re working solo or as part of a team, these tools are invaluable for maintaining high standards in your Git workflow.
FAQ’S
1. What is Commitizen?
Commitizen is a tool that helps you write your commit messages in a clear and consistent way. It asks you questions during the commit process to ensure your messages are easy to understand and follow a standard format.
2. What does Commitlint do?
Commitlint checks your commit messages to make sure they follow certain rules. If your message doesn’t meet the standards, Commitlint will alert you to fix it before you can complete your commit.
3. Why should I use Commitizen and Commitlint?
Using these tools ensures your commit messages are organized and follow a consistent format. This makes it easier for your team to understand changes and helps automate tasks like generating changelogs.
4. How do I set up Commitlint?
First, install Commitlint by running a command in your project. Then, create a configuration file (commitlint.config.js) to tell Commitlint what rules to follow.
5. What is Husky, and why do I need it?
Husky is a tool that lets you run checks, like Commitlint, automatically when you commit or push code. It helps catch mistakes before they make it into your project.
6. How do I set up Husky?
First, initialize Git in your project if you haven’t already. Then, install Husky and set it up to run Commitlint every time you make a commit.
7. How do I install Commitizen?
Install Commitizen as a development tool in your project. You’ll also need to add some configuration to your project’s settings to get it working.
8. How do I make a commit with Commitizen?
After setting up Commitizen, use the command yarn commit
instead of the usual git commit
. Commitizen will ask you some questions to help you create a properly formatted commit message.
9. What are the benefits of using conventional commits?
Conventional commits help you:
- Automatically create changelogs.
- Determine version updates based on the types of changes.
- Clearly communicate the purpose of changes to your team.
- Make it easier for others to contribute to your project.
10. What happens if my commit message doesn’t follow the rules?
If your message doesn’t follow the conventional commit rules, Commitlint will stop you from completing the commit. You’ll need to fix the message to match the required format before you can continue.