Git 提交信息规范
遵循规范的 Git 提交信息能极大提升代码的可维护性和团队协作效率。下面这个表格汇总了目前主流的提交信息规范核心要素:
| 规范要素 | 说明与要求 | 示例参考 |
|---|---|---|
| 提交类型 (Type) | 用特定关键字说明提交目的,如 feat(新功能)、fix(修复 bug)。 | feat, fix, docs |
| 范围 (Scope) | 可选,说明更改影响的部分,如模块、组件或文件。 | (login), (router) |
| 标题 (Subject) | 简短描述,建议使用祈使句(如”Add”而非”Added”),首字母大写,结尾不加句号,长度建议50字符以内。 | Add user authentication endpoint |
| 正文 (Body) | 可选,详细解释修改内容和修改原因(而非”如何修改”),每行约72字符。 | 说明问题背景、实现思路或注意事项。 |
| 脚注 (Footer) | 可选,用于记录元信息,如关联的问题跟踪编号(Issue)、此次提交的影响。 | Closes #123, BREAKING CHANGE: … |
| ✍️ 规范格式与编写技巧 | ||
| 一次符合规范的提交信息通常格式如下: |
1 | |
在编写时,你可以参考以下技巧:
标题行:确保标题行能清晰概括此次提交的目的和内容,便于快速浏览历史。
正文部分:在正文中,着重解释 “为什么” 要做出此次修改,包括修改的动机、与之前代码的差异等。如果修改复杂,可以使用列表来条理清晰地说明。
提交类型的选择:根据更改性质选择最贴切的类型。除了表格中提到的,常见的类型还有:
docs:仅文档更新。
style:代码风格调整(空格、格式化等,不改变逻辑)。
refactor:代码重构(既非新增功能,也非修复bug)。
test:增加或修改测试。
chore:构建过程或辅助工具的变动。
语言选择:建议使用英文编写提交信息,以便于全球开发者协作。但如果团队内部有约定,使用中文也是可行的。
特定规范要求:一些大型项目(如 Deepin)会在约定式提交的基础上,增加一些必填的元信息字段,例如 Log:(用于生成更新日志)。在为这类项目贡献代码时,需要留意其特定要求。
示例
以下是一些遵循规范的 Git 提交信息示例,从简单到复杂,涵盖不同场景:
🌟 基础示例
- 功能提交
1
2
3
4
5
6
7feat(auth): add OAuth2 login support
- Implement Google OAuth2 integration
- Add login page with OAuth2 options
- Create user session management
Closes #123 - Bug 修复
1
2
3
4
5
6fix(payment): correct currency conversion in checkout
Fix rounding error when converting from USD to EUR that caused
incorrect total amounts in shopping cart.
Fixes #456 - 文档更新🔧 进阶示例
1
2
3
4docs(readme): update installation instructions
Add detailed steps for Node.js version 18+ compatibility
Include troubleshooting section for common issues - 代码重构
1
2
3
4
5
6
7
8refactor(database): migrate from callbacks to async/await
- Replace callback hell with modern async syntax
- Improve error handling with try/catch blocks
- Maintain backward compatibility
BREAKING CHANGE: Database connection methods now return Promises
Migration guide: see MIGRATION.md - 样式调整
1
2
3
4
5
6style(components): reformat code with Prettier
Apply new code style rules:
- 2-space indentation
- Single quotes for strings
- Trailing commas in objects - 测试相关🚀 复杂场景示例
1
2
3
4
5
6
7
8test(api): add unit tests for user endpoints
Coverage for:
- User registration
- Profile update
- Password reset
Increase coverage to 90% for user module - 包含破坏性变更
1
2
3
4
5
6
7
8
9
10
11feat(config): restructure configuration system
- Move from JSON to YAML configuration files
- Consolidate environment variables
- Add validation schema
BREAKING CHANGE: Configuration file format changed from .json to .yml
Existing configs must be converted using tools/convert-config.js
Closes #789
See CONFIG_MIGRATION.md for details - 多模块修改
1
2
3
4
5
6
7
8fix: resolve critical security issues
- fix(auth): prevent SQL injection in login
- fix(api): validate input data in all endpoints
- fix(ui): sanitize user input in forms
Security: patch CVE-2023-12345
Requires immediate deployment - 持续集成相关📝 实际工作流示例
1
2
3
4
5
6
7
8chore(ci): migrate from Travis to GitHub Actions
- Add workflow for automated testing
- Implement code quality checks
- Configure deployment pipelines
Remove .travis.yml
Add .github/workflows/ci.yml - 功能分支合并
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17feat(checkout): implement multi-step checkout process
- Add address selection step
- Implement payment method options
- Create order review component
- Add progress indicator
Components:
- ShippingAddressForm
- PaymentMethodSelector
- OrderSummary
Testing:
- Add E2E tests for checkout flow
- Unit tests for new components
Closes #101, #102, #103 - 热修复提交⚠️ 对比示例:好 vs 差
1
2
3
4
5
6
7
8
9fix: emergency patch for data loss issue
Immediately revert commit a1b2c3d that caused:
- User profile data corruption
- Order history disappearance
Rollback changes until root cause is identified
Hotfix: v1.2.1
好的提交:需要改进的提交:1
2
3
4
5
6
7feat(search): add real-time autocomplete
- Implement debounced search input
- Add suggestion dropdown
- Cache frequent queries
Improves user experience by 40% based on UX tests💡 实用技巧1
2
3更新了一些东西
改了搜索功能,现在更好用了
使用 git commit -v 查看差异来编写更有意义的提交信息
小步提交 - 每个提交只做一件事
用正文解释”为什么” 而不是”做了什么”(代码已经展示了做了什么)
引用相关问题 使用 Closes #123, Fixes #456 等
Git 提交信息规范
https://zuyue200.github.io/2025/10/11/git-submission-specification/
