DEV Community

Ricardo Borges
Ricardo Borges

Posted on • Originally published at

How to set up Commitzen with Husky

Conventional commits specification contains a set of rules for creating an explicit commit history, which makes it easier to write automated tools on top of, for example, semantic release. You can manually follow this convention in your project or use a tool to assist you, such as Commitizen.

There are some ways to use Commitizen in your project, in this post, I will show you how to set it up with Husky, so whenever you run git commit, you'll be prompted to fill out any required commit fields at commit time.

To start, install Commitzen and Husky packages:

npm i commitizen husky --save-dev
Enter fullscreen mode Exit fullscreen mode

Next, initialize your project to use the cz-conventional-changelog adapter

commitizen init cz-conventional-changelog --save-dev --save-exact
Enter fullscreen mode Exit fullscreen mode

That command will do the following:

  1. Install the cz-conventional-changelog adapter npm module;

  2. Save it to package.json's dependencies or devDependencies;

  3. Add the config.commitizen key to the root of your package.json.

Finally, in the package.json file, set a Husky´s hook to trigger Commitzen on commit command

"husky": {
  "hooks": {
    "prepare-commit-msg": "exec < /dev/tty && npx cz --hook || true"
Enter fullscreen mode Exit fullscreen mode

And that´s it, you are all set. Make some changes to your code, run git commit, and follow the Commitzen instructions.

Top comments (1)

marcitpro profile image

Great post