目录
Git 相关
husky
husky是一个npm包,安装后,可以很方便的在package.json
配置git hook
脚本 。
https://typicode.github.io/husky/#/
https://www.npmjs.com/package/husky
安装
自动(推荐)
npx husky-init && npm install # npm
npx husky-init && yarn # Yarn 1
yarn dlx husky-init --yarn2 && yarn # Yarn 2
会修改 package.json
,并创建一个 pre-commit
hook 并在里面添加 npm test
作为hook。
添加其他的 hook 用 husky add
:
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
手动
安装:
-
Npm:
npm install husky --save-dev #安装
npx husky install #初始化git hook
npm set-script prepare "husky install" # 编辑package.json
// package.json
{
"scripts": {
"prepare": "husky install"
}
}
-
Yarn:
yarn add husky --dev
yarn husky install
创建hook:
npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit
卸载:
-
npm uninstall husky yarn remove husky && git config --unset core.hooksPath
Git hooks用法
Git 支持的所有钩子见下表(加粗的为常用钩子):
Git Hook | 调用时机 | 说明 |
---|---|---|
pre-applypatch | git am 执行前 |
|
applypatch-msg | git am 执行前 |
|
post-applypatch | git am 执行后 |
不影响git am 的结果 |
pre-commit | git commit 执行前 |
可以用git commit --no-verify 绕过 |
commit-msg |