git commit 提交规范
Conventional Commits
Conventional Commits 规范由 Angular 团队提出并推广,现已成为开源社区广泛采纳的提交标准。
基本格式:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]关键规则:
type和description必填,其余可选- scope 放在括号内,如
feat(auth): add login - 冒号后必须有一个空格
- 描述用英文现在时、首字母小写
- 含破坏性变更时,在 type/scope 后加
!,如feat!: drop Node 12 support,或在 footer 中写BREAKING CHANGE: xxx - footer 可引用 issue:
Closes #123或Refs #456
常用 type
| type | 含义 | 示例 |
|---|---|---|
| feat | 新功能 | feat: add login page |
| fix | 修复 Bug | fix: correct validation error |
| docs | 文档变更 | docs: update README |
| style | 格式(不影响逻辑) | style: format with prettier |
| refactor | 重构 | refactor: extract helper |
| test | 测试 | test: add unit test for login |
| chore | 构建/工具变更 | chore: update dependencies |
| perf | 性能优化 | perf: optimize image loading |
| ci | CI 配置变更 | ci: add deploy workflow |
工具配合
- commitlint — 校验 commit message 是否符合规范
- husky — Git hooks 管理(pre-commit 跑 lint-staged)
- lint-staged — 只 lint 暂存区文件
- standard-version — 自动生成 CHANGELOG + 版本号
最小可用配置
bash
npm i -D @commitlint/cli @commitlint/config-conventional husky lint-stagedcommitlint.config.js:
js
module.exports = { extends: ['@commitlint/config-conventional'] };package.json:
json
{
"husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } },
"lint-staged": { "*.{js,vue}": ["eslint --fix", "prettier --write"] }
}scope 命名规范
- 按模块:
feat(auth),fix(checkout),refactor(api) - 无明确模块时可省略 scope:
chore: update deps - 多 scope 用逗号:
feat(auth,user): add SSO login