Back to blog

Published on Monday, July 14, 2025

How to Set Up Commitlint to Enforce Clean Git Commit Messages

How to Set Up Commitlint to Enforce Clean Git Commit Messages

Introduction

Writing consistent and meaningful Git commit messages improves collaboration, code readability, and changelog generation. Commitlint helps enforce rules on your commit messages, making sure your team follows a standardized format like Conventional Commits.

In this post, you’ll learn how to install and configure Commitlint in your project in just a few steps.

Installation

pnpm add -D @commitlint/{cli,config-conventional}
# OR
npm install --save-dev @commitlint/cli @commitlint/config-conventional

Create Commitlint Config

Create a file named commitlint.config.js at the root of your project:

// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional'],
};

This uses the popular Conventional Commits format (like feat:, fix:, etc.).

If you want to enforce commit message rules automatically on every commit:

Install Husky

pnpm add -D husky
npx husky install

Enable Git Hooks

Add this to your package.json:

"scripts": {
  "prepare": "husky install"
}

Then run:

pnpm run prepare

Add Commit-msg Hook

npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1'

This hook will now validate every commit message automatically.

Commit Message Examples

✅ Valid❌ Invalid
feat: add loginupdated login
fix: crash bugbug fixed
docs: update FAQreadme changes

Only commits starting with allowed types (feat, fix, docs, etc.) will be accepted.

Optional: Customize Rules

Need more control? Edit commitlint.config.js:

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2, 'always',
      ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore']
    ],
  },
};

Pro Tip: Use Commitizen for Guided Commit Prompts

Install Commitizen for an interactive commit prompt:

pnpm add -D commitizen
# OR
npm install -D commitizen

Add to package.json:

"scripts": {
  "commit": "cz"
}

Now run pnpm commit to be guided through creating a valid commit message.

Conclusion

That’s it! You’ve now set up Commitlint to enforce clean and consistent Git commit messages in your project. This improves collaboration, automation, and overall code hygiene.

Start using it today to level up your team’s workflow! 💪