643 字
3 分钟
Git提交规范
2025-12-09

Git 提交规范整理#

提交消息结构#

一个标准的 Git 提交消息由以下部分组成:

  1. 类型(type): 表示提交的类别。
  2. 范围(scope,可选): 指定更改影响的模块或范围,用括号包裹。
  3. 主题(subject): 简短描述更改内容,通常以动词开头,50-72 字符以内。
  4. 正文(body,可选): 详细说明更改的原因和细节,每行不超过 72 字符。
  5. 脚注(footer,可选): 关联问题或其他元信息,例如 issue ID。

格式模板:

<类型>(<范围>): <主题>
<空行>
<正文>
<空行>
<脚注>

常见类型#

以下是常见的提交类型及其含义:

  1. feat: 新功能或特性。
  2. fix: 修复 bug。
  3. docs: 修改文档。
  4. style: 调整代码格式(不影响逻辑)。
  5. refactor: 代码重构(不新增功能或修复 bug)。
  6. test: 添加或修改测试。
  7. chore: 杂项(依赖更新、配置调整等)。
  8. perf: 性能优化。
  9. ci: 持续集成相关更改。
  10. revert: 回退之前的提交。

逐一示例#

以下为每种类型提供一个具体示例,包含完整的结构(类型、范围、主题、正文、脚注)。

1. feat – 新功能#

feat(search): add autocomplete suggestions
Implement real-time suggestions for search input field.
Fetch top 5 matching results from backend API as user types.
Enhance UX with keyboard navigation support.
Closes #101

2. fix – 修复 Bug#

fix(auth): resolve session timeout crash
Fix bug where app crashes on session expiration.
Add null check for expired token and redirect to login page.
Ref #89

3. docs – 文档变更#

docs(api): update endpoint documentation
Add details for new POST /users endpoint in API spec.
Include request/response examples and error codes.

4. style – 代码样式调整#

style(css): reformat stylesheets with consistent spacing
Apply 2-space indentation across all CSS files.
Align properties alphabetically for better readability.

5. refactor – 代码重构#

refactor(cart): extract discount logic to separate module
Move discount calculation from Cart class to DiscountUtil.
Improve code modularity and testability.

6. test – 测试相关#

test(payment): add integration tests for checkout flow
Cover successful payment and failure scenarios.
Mock third-party payment gateway responses.

7. chore – 杂项#

chore(deps): update react to v18.2.0
Upgrade React dependency in package.json.
Run tests to ensure compatibility with existing components.

8. perf – 性能优化#

perf(api): cache frequent database queries
Add Redis caching layer for user profile lookups.
Reduce average response time by 30% for GET /users/:id.

9. ci – 持续集成#

ci: configure deployment pipeline in CircleCI
Add deploy step to push production builds to AWS.
Set up environment variables for secure credentials.

10. revert – 回退提交#

revert: undo feat(search): add autocomplete suggestions
Revert commit abc123 due to performance issues with large datasets.
Restore previous search behavior until optimization is ready.

总结与建议#

  • 主题简洁: 控制在 50-72 字符,使用动词开头(如 “add”、“fix”)。
  • 正文清晰: 说明“为什么”和“做了什么”,便于理解上下文。
  • 脚注实用: 用 Closes #IDRef #ID 关联任务或问题。
  • 工具支持: 可以用 commitizen 或 Git 钩子(如 husky)强制规范。
Git提交规范
https://fuwari.vercel.app/posts/git-commit-spec/
作者
asm2apex
发布于
2025-12-09
许可协议
CC BY-NC-SA 4.0