husky: commitizen does not work with 5.0.9

Hi guys,

Husky Version 5 breaks the pre-commit-message hook for Commitizen.

I think this is a common problem for Husky V5 migration.

(If like me you are used to simply adding husky and commitizen to a project without knowing of the changes in new Husky 5, then this may save you time.)

SOLUTION (1) - Husky v4:

STEP 1: Roll back to Husky 4.3.8

SOLUTION (2) - Husky v5:

STEP 1: Replace the following hook in package.json:

( This is the Husky Version 4 hook specified by commitizen, see: https://github.com/commitizen/cz-cli )

"husky": {
		"hooks": {
			"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"
		}
	},

STEP 2: With a external hook by executing the following (which create a Husky V5 hook in a separate file):

npx husky add .husky/prepare-commit-msg "exec < /dev/tty && git cz --hook || true"

You hook will be output here:

.husky/prepare-commit-msg

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

@lucaslamar you won’t be able to surpress default behavior of git commit with the comand of yours (if no message is provided, git will open a default editor to let you type a message - in your case it is nano)

Use the answer above by @ricovilela to trigger commitizen on git commit


#!/bin/sh
#[.husky / prepare-commit-msg]
. "$(dirname "$0")/_/husky.sh"

exec < /dev/tty && yarn cz --hook || true
# OR
# exec < /dev/tty && npx cz --hook || true

However, if you have only the prepare-commit-msg hook set up, I advise you to use yarn cz command directly. Otherwise there will be always a step, where your default editor for your OS opens right after commitizen finishes, because that’s how this hook works.

I need to migrate from version 4 to 6, I ran the commands and put

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

exec < /dev/tty && git cz --hook || true

but when executing the command git commit it happened image

@ahutchings @azu @alexeyten

I was able to resolve this by running the husky-4-to-5 migration tool in an existing project (encouraged by the husky migration docs), which basically performs the steps described by @paulmboyce . It copied over my hooks from the husky section of package.json into applicable files in .husky, so the section in package.json is no longer needed.

I avoided the global install and got this working by switching git cz to npx cz in the prepare-commit-msg hook:

exec < /dev/tty && npx cz --hook || true

Specs

Module Version(s)
husky 4.3.8 -> 5.1.1
commitizen 4.2.3
cz-emoji 1.3.1

Working on Windows 10 and WSL Ubuntu 20.04

someone has version 7.0.1? I’m having the same problem with @lucaslamar My interactive terminal opens. ask the questions. but instead of committing at the end it opens the text editor. my hook file is the same as what @zorgick put above.

I have the same problem 🥲, is there any solution better then alias commit with a default message, I think it’s a hack.

And after add the hook, run npx cz will call cz twice.

@miguelsmuller It is unclear what you want to achieve. If you want to add an alias for git commit -m 'a' in a single repo, execute this command in your repo:

git config --local alias.cm "commit -m 'a'"
# use it 
git cm

This will create an alias in ./.git/config, that will work only in this repo

Just wanna point out that npx cz is a similar workaround to installing git-cz locally for a project. Neither of which should need to happen.