Managing Unified Git Commit Messages with Commitizen

A guide to setting up and using Commitizen for consistent Git commit messages following Conventional Commits, with installation steps and usage examples.

In team development, consistent commit messages make project history easier to understand and are useful for automated changelog generation and CI/CD triggers. Commitizen is a tool for effectively unifying Git commit messages, assisting in creating commit messages through interactive prompts.

What is Commitizen

Commitizen is a CLI tool that enforces commit message formatting and helps developers create consistent messages. Since each element of the commit message (type, scope, description, etc.) can be entered interactively, there is no need to memorize the format manually.

Installation Steps

1. Installing Commitizen

First, install the Commitizen CLI globally.

npm install -g commitizen

This makes the git cz command available.

2. Installing a Commit Message Convention Adapter

When using Commitizen, you need an “adapter” that defines the format for commit messages. Here, we’ll use cz-conventional-changelog, which creates commit messages compliant with the Conventional Commits convention.

npm install -g cz-conventional-changelog

Next, create a configuration file so that Commitizen uses this adapter.

# Create a .czrc file and specify the adapter path
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

Note: This .czrc file is created in the user’s home directory. If you want to use different adapters for each project, you can also create a package.json at the project root and configure config.commitizen.path.

Usage

Use the git cz command instead of the regular git commit command to create commit messages.

git cz

Running this command displays an interactive prompt where you can select and enter each part of the commit message.

cz-cli@4.3.0, cz-conventional-changelog@3.3.0

? Select the type of change that you're committing: (Use arrow keys)
> feat:     A new feature
  fix:      A bug fix
  docs:     Documentation only changes
  style:    Changes that do not affect the meaning of the code (white-space,
formatting, missing semi-colons, etc)
  refactor: A code change that neither fixes a bug nor adds a feature
  perf:     A code change that improves performance

By following the prompt instructions and entering the commit type (feat, fix, docs, etc.), scope, short description, and detailed description, a commit message following the Conventional Commits convention is automatically generated.

Main Conventional Commits Types

  • feat: New feature addition
  • fix: Bug fix
  • docs: Documentation-only changes
  • style: Code style changes (formatting, semicolons, etc.)
  • refactor: Refactoring (code changes that neither fix bugs nor add features)
  • perf: Performance improvement
  • test: Adding or modifying test code
  • build: Changes to the build system or external dependencies
  • ci: CI/CD-related changes
  • chore: Other miscellaneous changes (build process, auxiliary tools, etc.)
  • revert: Reverting a previous commit

By introducing Commitizen, you can improve the quality of commit messages across the entire team and manage the project more efficiently.