question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

precommit runs twice when using commitizen

See original GitHub issue

I’m using Husky. I have:

"scripts": {
   "precommit": "lint-staged --verbose",
   "commit": "git-cz",
},
"lint-staged": {
    "*.js": [
      "prettier --single-quote --semi=false --write",
      "eslint --fix",
      "git add"
    ]
},
"config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
},

At first I thought perhaps the precommit script isn’t needed anymore, so I remove the it and lint-staged doesn’t run at all. Then I decided to commit without commitizen and realized that’s where the problem is. lint-staged is called once before the commitizen command line form appears, and again after you submit it. Is that the expected behavior? What can be done about it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
Hotellcommented, Jun 13, 2018

this is fixed with latest husky ✌️

yarn add husky@next
"scripts": {
-"cz": "git-cz",
+"commit": "git-cz",
-"precommit": "lint-staged"
},
+"husky": {
+   "hooks": {
+      "pre-commit": "lint-staged"
+   }
+ }
3reactions
jcarroll2007commented, Oct 31, 2018

@wearefridayhutber I get the same. My approach has been to simply run a specific script before husky.

"devDependencies": {
   "cz-conventional-changelog": "^2.1.0",
   "husky": "^0.14.3",
},
"scripts": {
  "commit": "lint-staged && git-cz --no-verify",
},
"husky": {
   "hooks": {
     "pre-commit": "lint-staged"
   }
},

The --no-verify flag prevents husky from being run after commitizen.

Or


"devDependencies": {
   "cz-conventional-changelog": "^2.1.0",
   "husky": "^0.14.3",
},
"scripts": {
  "pre-commit": "lint staged",
  "commit": "yarn run pre-commit && git-cz --no-verify",
},
"husky": {
   "hooks": {
     "pre-commit": "yarn pre-commit"
   }
},

This approach ensures that husky is run before commitizen (but not after) and before a vanilla git commit

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commitizen and husky seem to be triggering the pre-commit ...
I am trying to configure commitizen with husky and when I commit using a custom script from package.json commit gets triggered twice.
Read more >
Developers - should husky be running precommit hooks twice
should husky be running precommit hooks twice: once before commitizen wizards and again after?
Read more >
Supported hooks - pre-commit
forbid-autopkg-trust-info - This hook prevents AutoPkg recipes with trust info from being added to the repo. munki-makecatalogs - This hook runs the " ......
Read more >
How To Setup Husky, Lint Staged, And Commitizen On A Next ...
As I said before about Git Hooks, we can use these hooks to run custom ... open it and create a new file...
Read more >
Set up ESLint, Prettier, Commitizen, Husky and friends in 2022
Upon running this command, Husky will add a .hooks directory with a sample pre-commit hook, and modify the package.json with a prepare ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found