Husky: Git hooks, which use hooks to execute commands on Git. Executing custom scripts can prevent bad commit/push issues from using Git hooks.

Husky making address

Install HusKY (currently up to V7.0.1)

npm install husky --save-dev
or
yarn add husky -D
Copy the code

Add the prepare script in packgae.json

{"prepare": {"prepare": "husky install"}} The prepare script is automatically executed after NPM install (with no parameters). The husky install command will create the. Husky/directory and specify it as the directory where git hooks are located.Copy the code

Add the git hooks

npx husky add .husky/pre-commit "npm run test"
Copy the code

After running this command, we will see a new shell script named pre-commit in the. Husky/directory. This means that the pre-commit script will be executed before git commit. The pre-commit script content is as follows:

#! /bin/sh . "$(dirname "$0")/_/husky.sh" npm run testCopy the code

Configure the hooks in package.json

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint -E $HUSKY_GIT_PARAMS"
    }
  },
Copy the code

Add hooks through husky Add

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
Copy the code

Check mailboxes and Lint-staged hooks in pre-commit hooks

. "$(dirname "$0")/_/husky.sh" EMAIL=$(git config user.email) if [[ ! $EMAIL =~ ^[.[:alnum:]]+@qq\.com$ ]]; then echo "Your git information is not valid"; echo "Please run:" echo ' git config --local user.name "<Your name in qq>"' echo ' git config --local user.email "<Your alias>@qq.com"' exit 1; fi; Yarn Lint-staged echo 'Check lint specifications'Copy the code

See the GitHook tool

See husky usage summary