Ricardo Borges

Ricardo Borges

Personal blog

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:

1npm i commitizen husky --save-dev

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

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

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

1"husky": {
2  "hooks": {
3    "prepare-commit-msg": "exec < /dev/tty && npx cz --hook || true"
4  }
5}

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

github iconlinkedin icon
Buy Me A Coffee