1. commit message
-
commit message 分3個部分:head, body, footer
- head: <type> (scop): subject
- type: feat, fix, docs, style, refactor, test, chore, revert, perf, build
- score: 影響範圍
- subject: 簡短描述(動詞開頭,首字母小寫,結尾不加.)
- body: 詳細描述
- footer: (1) 不兼容改動;(2) 關閉issue
- head: <type> (scop): subject
-
revert 有特殊的格式
# head: revert: <要被撤銷的commit的head> # body: This reverts commit <commit-hash>. # eg: revert: docs add README.md This reverts commit a2d04c0b914785e4ff0cdf4baeea84d8611c7a61.
2. 一些規範commit message的工具
-
commitizen:輔助編寫規範的commit message
# 安裝commitizen cnpm install -g --save-dev commitizen # 生成package.json文件 npm init --yes # 設置支持commit message格式 commitizen init cz-conventional-changelog --save --save-exact
使用
git cz
代替git commit
type介紹type detail 說明 feat A new feature 新功能 fix A bug fix 修復 bug docs Documentation only changes 文檔修改 style Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc) 格式(不影響代碼運行的變動) refactor A code change that neither fixes a bug nor adds a feature 重構 perf A code change that improves performance 提高性能 test Adding missing tests or correcting existing tests 添加缺失測試或更正現有測試 build Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) 依賴的外部資源變化 chore Other changes that don’t modify src or test files 構建過程或輔助工具的變動 revert Reverts a previous commit 恢復先前的提交 -
validate-commit-msg:在每次commit前檢驗縮寫的message是否符合規範
# 安裝validate-commit-msg npm install --save-dev validate-commit-msg # 安裝ghooks cnpm install ghooks --save-dev
在 package.json 配置 ghooks。在config中添加如下信息
"config": { "ghooks": { "commit-msg": "validate-commit-msg" }, "validate-commit-msg": { "types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"], "scope": { "required": false, "allowed": ["*"], "validate": false, "multiple": false }, "warnOnFail": false, "maxSubjectLength": 100, "subjectPattern": ".+", "subjectPatternErrorMsg": "subject does not match subject pattern!", "helpMessage": "", "autoFix": false } }
- change log:目前還未使用,需要再翻查
參考1:Git 提交的正確姿勢:Commit message 編寫指南
參考2:git commit 規範工具