Improve your Git Commit workflow

Sandro Marques
Oct 19, 2019 · 2 min read

The following steps lead you to improve your git commit messages as well as automating changelog and versioning.

Goals

Installation

  • husky will trigger the commitlint on each commit
  • commitlint checks if your commit messages meet the Conventional Commits format
  • commitizen and cz-conventional-changelog helps format commit messages with a series of prompts
  • standard-version will update CHANGELOG.md, bump the version and generate a new tag

Step 1 — Install husky, commitlint, cz-conventional-changelog and standard-version locally

yarn add --dev husky @commitlint/cli @commitlint/config-conventional cz-conventional-changelog standard-version

Step 2 — Install commitizen globally

sudo yarn global add commitizen

Step 3 — Update package.json

{
...
"scripts": {
"release": "standard-version"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}

Step 4 — Create a commitlint.config.js file in your project root directory

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

Step 5 — Configure commitizen

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

Usage

Basically, instead of typing git commit now you type git cz which will open a wizard and help you write a standardized message.

Commitizen template
Commitizen template
Commitizen template

Commit with commitizen

Using commitizen to prompts a wizard

git add .
git cz
yarn release
git push --follow-tags

Pro tip

Run this command in your first release to prevent bumping the version in package.json

yarn release -- --first-release

Normal commit command

You can still use git commit ... but the commit will fail if the commit message is not properly formatted.

git add .
git commit -m "feat(blog): add comment section"
yarn release
git push --follow-tags

Commit but skip changelog, bump version and tag

git add .
git cz
yarn release --skip.changelog --skip.bump --skip.tag
git push

Credits

Conclusion

With these four packages, you can easily write meaningful commit messages with Conventional Commits. In addition, you will get the changelog automatically generated and with the correct version numbers. All this without any effort. This is awesome.

Sandro Marques

Written by

Frontend Engineer | Remote Worker | Main focus on JavaScript and React but I also love PHP | Coding is awesome! | sandromiguel.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade