From Chaos to Clarity: How Conventional Commits Revolutionize Version Control

Well, we all are in a hurry especially in our projects to get them ready to show the world how great of a developer we are, all while neglecting an essential aspect of the whole process which is the commits using version control systems!

Having a good commit history is as important as writing good quality documented code & believe me, in the future, you will thank yourself if you implement them both in your projects.

The Problem

Look carefully and try to understand the difference between the two commit histories we see below:

Fig 1 - Commit history of one of my projects

Both the commits histories would seem okay to a novice eye but an experienced dev knows how better and more useful the second one is. We see a better description of what happened in that commit, whether it was a feature added, docs updated, or any bug fix.

Of course, now you may be curious how we transition from the first commit history to the better second one. Let's understand the same!

The Solution

The commit pattern we observe in the second commit history is known as Conventional Commits which is a lightweight convention on top of commit messages, providing rules for creating such commit history.

The above image is a screenshot from Conventional Commit Docs explaining everything we need to know but who wants to go through the pain of understanding and then implementing it yourself?

There's a tool called Commitizen that makes the job easy for us! It provides a CLI-based commit writing method taking in all the inputs required for a standard conventional commit to ensure a better commit history.

Setting up commitzen

Follow the given steps to set commitzen for your projects

Step 1: Install Git

If you haven't already, install Git on your system. You can download it from the official website: Git Downloads.

Step 2: Initialize a Git Repository

Navigate to your project directory in your terminal or command prompt and initialize a new Git repository by running:

git init

Step 3: Install Commitizen

Commitizen is a command-line tool that helps you create conventional commit messages. You can install it globally using npm:

npm install -g commitizen

Step 4: Setting up commitzen in your repository

Move inside your project's root directory and use the below command to initialize your project to use the cz-conventional-changelog adapter:

commitizen init cz-conventional-changelog --save-dev --save-exact

Also, add the config.commitizen key to the root of your package.json file

"config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }

Step 5: Using commitzen

Use 'git cz' instead of 'git commit' to use commitzen to structure your commits

git add .
git cz

That's it now you'll surely have a better, maintainable, and useful commit history.

Thank you for reading the blog. I'd love to hear your thoughts and insights, so feel free to share your comments. Don't forget to hit the follow button to stay tuned for more lessons learned and experiences in the development world.

Happy learning!

20
Subscribe to my newsletter

Read articles from Ayush Singh Kushwah directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayush Singh Kushwah
Ayush Singh Kushwah

Developer