通過commit-message規範評審,在業界經常使用的:atom,eslint和Angular等規範中, 能夠選擇最經常使用的Angular規範做爲咱們平常項目中的提交規範node
每條提交記錄包含三個部分:header,body和footergit
<header> <BLANK LINE> <body> <BLANK LINE> <footer>
Commit Message Headergithub
<type>(<scope>): <short summary> │ │ │ │ │ └─⫸ Summary in present tense. Not capitalized. No period at the end. │ │ │ └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core| │ elements|forms|http|language-service|localize|platform-browser| │ platform-browser-dynamic|platform-server|router|service-worker| │ upgrade|zone.js|packaging|changelog|dev-infra|docs-infra|migrations| │ ngcc|ve │ └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test
其中<type>和<summary>是必要的 <scope>是可選的shell
經過commitizen進行交互式提交,husky + commit-msg hook進行提交校驗,cz-customizable來自定義交互提交流程和文案
安裝commitizennpm
npm i -D commitizen
package.json中添加對應的npm scriptjson
"commit":"cz"
改動添加到暫存區後執行commit提交api
npm run commit
安裝husky , commitlint 和 符合angular提交規範的配置bash
npm i -D husky commitlint @commitlint/config-conventional
添加git hooks工具
npx husky install
package.json中添加prepare的npm hook, 在每次install自動執行(yarn v2+不支持prepare)性能
"prepare": "husky install"
執行添加commit-msg hook
若是使用husk v4.x版本(推薦升級到新版本),直接在package.json中或.huskyrc.json中新增commit-msg鉤子便可
package.json
"husky": { "hooks": { "commit-msg": "commitlint --edit $1" } }
.huskyrc
,.huskyrc.json
,.huskyrc.js
或husky.config.js
"hooks": { "commit-msg": "commitlint --edit $1" }
若是使用husky v6+版本,須要添加對應的shell調用方式(husky v6對添加方式作了改動,因此沒法經過添加配置到package.json中運行)
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
添加commintlint配置(也能夠放到package.json中指定)
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
package.json中添加commitlint配置
"commitlint": { "extends": [ "@commitlint/config-conventional" ] }
commitizen提供的交互式默認是英文的,若是改爲中文或者對交互流程進行改動,可使用cz-customizable進行擴展和自定義
安裝cz-customizable
npm i -D cz-customizable
package.json中添加配置
"config": { "commitizen": { "path": "node_modules/cz-customizable" }, "cz-customizable": { "config": ".cz-config.js" } }
.cz-config.js就是cz-customizable配置的具體文件了,能夠參考CZ-Config-Example並進行改動, 能夠把文案翻譯成中文,自定義修改提示等。
也能夠經過fork cz-customizable建立內封配置文件的npm包
npm i -D your-own-package
"config": { "commitizen": { "path": "node_modules/your-own-package" } }
配置文件能夠自定義交互內容,好比能夠只保留type scope breakchange confirm
配置文件中設置skipQuestions: ['body','customScope','scope','footer'],便可忽略其餘選項
allowBreakingChanges: ['feat', 'fix'], 僅在feat和fix時提示 breakchange
經過npm script進行commit,若是eslint沒有經過(在pre-commit 鉤子中作了eslint檢測),可是又想提交能夠經過加'––'來向npm script傳參
npm run commit -- --no-verify # or npm run commit -- -n