Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active November 29, 2017 20:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bahmutov/a15d49b3fe503fb546fb to your computer and use it in GitHub Desktop.
Save bahmutov/a15d49b3fe503fb546fb to your computer and use it in GitHub Desktop.
Setting up git message validation in NPM project

Setting up Git message validation hook

Install pre-git for handling Git hooks and commitizen for helping format the commit messages

npm install pre-git commitizen cz-conventional-changelog --save-dev

The package cz-conventional-changelog will validate the message using AngularJS-style guideline: <type>(<scope>): <subject>

Set the commit message hook command to validate the message using AngularJS-style guidelines in package.json

"config": {
  "pre-git": {
    "commit-msg": "validate-commit-msg"
  }
}

Configure the npm run commit command

"scripts": {
  "commit": "git-cz"
},
"czConfig": {
  "path": "node_modules/cz-conventional-changelog"
}

That is it. Now instead of running the git commit just use npm run commit to commit the staged files. Answer simple questions and get useful and valid commit messages.

You can always skip commit message validation (and pre-commit hook too) by using -n or --no-verify command line argument to git commit

Additional info:

Update 1

I added validator and helper to pre-git module, thus all you need to do is to install npm install pre-git --save-dev - the hooks and everything will be configured automatically. Then you should use git add . && npm run commit command to commit code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment