Using conventional commits to automate explicit commit stories

I'm constantly trying to improve as a developer. This entails refining the coding process and automating coding-related chores. My ability to write commit messages is one area where I have made progress.

When pressed for time, one terrible tendency is to disregard documentation. When missing paperwork is found, it becomes significant.

That's one thing I'm working on being better at. How can we make better commit messages since they are a form of documentation that is most frequently disregarded?

Enhance your git log with conventional commits introduced me to Conventional Commits. I enjoyed the essay and have been manually writing traditional commits for the past few days.

In all of my projects, I wanted to automate the enforcement of traditional commit messages. I used Google to get the tools I required to set it up for all commit messages globally:

Writing a conventional commit message is made easier with Commitizen and Commitlint. GIT global hooks can be used to verify that each commit is formatted correctly. fail if the commit message doesn't adhere to the guidelines

Commitizen

The useful script Commitizen prompts and constructs the components of a typical commit statement.

Installing it is the first step: (Extract from README)

the command npm install -g commitizen

Install the commitizen adaptor you choose worldwide, such as cz-conventional-changelog:

The command is: npm install -g cz-commitizen-changelog

Make a .czrc file with the commitizen adapter's path in it.

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

Instead of using 'git commit' after installing Commitizen, use 'git cz' to create a commit.

First, the type of commit will be requested:

Commit Type

The commit subject, description, breaking changes, and open problems are then requested:

Commit Details

We can move on to the following step now that the commit is prepared.

Commitlint

A tool called Commitlint will check your commit message to see if it complies with a standard you specify.

It's just as simple to install globally as Commitizen. Some configuration is required for items not covered in the README.

Install the CLI and the desired configuration:

npm install -g @commitlint/cli @commitlint/config-conventional

Create a global config file:

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > ~/commitlint.config.js

That's all. You can test it to make sure the install is correct:

echo 'must fail' | commitlint

If you want to see a passing message try:

echo 'fix(broken links) fix broken links in homepage'

We are now prepared to connect everything together.

Git Hook

Until it is incorporated into a git hook, commitlint doesn't do much on its own. If the commit message deviates from the norm, we shall reject it from within this hook.

Similar to the earlier packages, I wanted to configure this once for all of my projects rather than having to go through each one individually. I consequently implemented a global git hook. (Subject for another post)

I had global git hooks set up, so I added a commit-msg file with a simple script. This script compares it with commitlint, and fails if the message is not following the rules:

#!/bin/bash

cat $1 | commitlint

Conclusion

You may enable conventional commit in your workflow with these three simple actions. If you are in the practice of making atomical commits, you will benefit the most from them. After utilizing this method for a while, you'll see its beauty more clearly. Run git log —one-line to view the gorgeous list of commits with all the relevant information.