Logo Smiley

My Portfolio

Importance of good commit message

Published on

May 1, 2024

Linux

Git

Commit message

In this article, we’ll delve into the

  1. Importance of crafting a good commit message, exploring its significance.
  2. Simple rules to follow for meaningful messages.
  3. Additionally, we’ll discover how tools like commitizen can streamline the process, making it easier for developers to maintain clear and informative commit histories.

Why Having a Good Commit Message is Important

Before we dive into the details, let’s address the fundamental question: why does a good commit message matter?

These points highlights the importance of investing time and effort into crafting good commit messages. Now, let’s outline some simple rules to ensure your commit messages are clear and meaningful.

Simple Rules for a Good and Meaningful Commit Message

According to CBEAMS, there are seven rules to follow for great commit messages. However, for simplicity, we’ll focus on the essentials and good enough:

  1. Subject and Body: Each commit message should have a subject and a body, separated by a blank line.
  2. Conciseness: Limit the subject line to 50 characters and the body to 72 characters.
  3. Imperative Mood: Use the imperative mood in the subject line.
  4. Focus on What and Why: Use the body to explain what and why the changes were made, rather than how.

Simplifying Commit Messages with Commitizen

Now, let’s explore how tools like commitizen can simplify the process of writing good commit messages.

To demonstrate, let’s set up commitizen for a simple Node.js project:

mkdir commitizen-demo # create project directory

cd commitizen-demo # change to project directory

npm init -y # Initialize Node.js project

git init # Initialize git repository

Once the project is initialised, will mimick a real world code repository with a README file and src directory that would usually contain all the source code.

.
├── README.md
├── package.json
└── src
    └── index.js

Next, install commitizen globally and initialize the project with the cz-conventional-changelog adapter:

npm install -g commitizen # Install commitizen globally

commitizen init cz-conventional-changelog --save-dev # Initialize project with commitizen

Finally, integrate commitizen with husky to execute it automatically when committing changes:

npm install --save-dev husky # Install husky

npx husky init # Initialize husky

Then, add the following script to .husky/_/prepare-commit-msg:

#!/bin/bash

exec < /dev/tty && node_modules/.bin/cz --hook || true

With commitizen set up, you’ll now see an interactive prompt when committing changes, ensuring consistent and meaningful commit messages.

Commitizen preview

Commitizen offers various adapters for different conventions, making it adaptable to different project setups. We saw how to configure cz-conventional-changelog, similiarly we can configure cz-conventional-changelog-for-jira. This allows us to attach JIRA ID for each commit message.


In my next article, we'll explore using commitizen for Python projects, stay tuned.

That’s all for now! Remember, a good commit message goes a long way in fostering effective collaboration and maintaining a healthy codebase.

References