conventional-git

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Conventional Commits & Branch Naming

Conventional Commits 分支命名与提交消息规范

Follow Conventional Commits v1.0.0 for both branch names and commit messages — consistent naming lets tools auto-generate changelogs, enforce SemVer bumps, and filter history by concern.
分支名称和提交消息均需遵循Conventional Commits v1.0.0规范——统一的命名规则可让工具自动生成变更日志、强制执行SemVer版本号升级,并按关注点过滤历史记录。

Branch Naming

分支命名规则

Format:
<type>/[issue-]<description>
— lowercase, hyphens only, no special chars except
/
.
feat/user-authentication
feat/42-user-authentication
fix/login-race-condition
fix/87-login-race-condition
docs/api-reference-update
refactor/payment-module
Prefix with the issue number when one exists — GitHub and GitLab auto-link it and it makes
git log
immediately traceable to the tracker. Keep the description under 50 characters — most git UIs truncate branch names in lists around that length. Match the type to the work you're doing — this is the contract readers use to understand the branch purpose at a glance.
NEVER include
worktree
in a branch name — git worktrees are a local checkout mechanism, not a branch concept; the name would leak implementation details into the remote and confuse other contributors.
格式:
<type>/[issue-]<description>
—— 全部小写,仅使用连字符,除
/
外无特殊字符。
feat/user-authentication
feat/42-user-authentication
fix/login-race-condition
fix/87-login-race-condition
docs/api-reference-update
refactor/payment-module
若存在对应议题编号,需在分支名前缀中加入——GitHub和GitLab会自动关联议题,且能让
git log
直接追溯到议题跟踪器。描述部分需控制在50字符以内——大多数Git UI会在列表中截断超过该长度的分支名。分支类型需与工作内容匹配——这是让阅读者一眼理解分支用途的约定。
绝对不要在分支名中包含
worktree
——Git工作区是本地检出机制,不属于分支概念;将其加入远程分支名会暴露实现细节,导致其他贡献者困惑。

Commit Message Format

提交消息格式

<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Types:
TypeSemVerWhen
feat
MINORNew feature
fix
PATCHBug fix
docs
Docs only
style
Formatting, no logic change
refactor
Restructure, no feature/fix
perf
Performance improvement
test
Add/fix tests
build
Build system, deps
ci
CI config
chore
Anything else (not src/test)
revert
Reverts a previous commit
Rules:
  • Subject line ≤ 72 characters — git log and GitHub/GitLab UIs silently truncate longer subjects
  • Imperative mood: "add" not "added" — reads as an instruction, not a history log
  • No capital letter, no trailing period — enforces uniform parsing by changelog tools
  • Body separated by blank line — parsers split header/body at the first blank line
  • Breaking changes: use
    !
    after type/scope, or add
    BREAKING CHANGE:
    footer (triggers MAJOR bump) — body-only descriptions are invisible to changelog tools
  • revert
    commits SHOULD include
    This reverts commit <hash>.
    in the body —
    git revert
    generates this automatically; don't strip it
  • NEVER add a Claude signature, AI agent attribution, or
    Co-authored-by
    trailer for Claude or any other AI agent to commits
Examples:
feat(auth): add JWT token refresh
fix: prevent race condition on concurrent requests

Introduce request ID and reference to latest request.
Dismiss responses from stale requests.
refactor!: drop support for Go 1.18

BREAKING CHANGE: Go 1.18 no longer supported; uses stdlib APIs from 1.21+
<type>[可选 scope]: <描述>
[可选正文]
[可选页脚]
类型说明:
类型SemVer 适用版本使用场景
feat
MINOR(次版本)新增功能
fix
PATCH(修订版)修复Bug
docs
仅修改文档
style
代码格式调整,无逻辑变更
refactor
代码重构,不新增功能或修复Bug
perf
性能优化
test
添加或修复测试用例
build
构建系统或依赖项修改
ci
CI配置修改
chore
其他非源码/测试相关修改
revert
回滚之前的提交
规则要求:
  • 主题行长度≤72字符——Git日志及GitHub/GitLab UI会自动截断过长的主题
  • 使用祈使语气:用“add”而非“added”——读起来像指令而非历史记录
  • 首字母不大写,末尾无句号——确保变更日志工具能统一解析
  • 正文与主题行之间用空行分隔——解析器会以第一个空行作为主题与正文的分界
  • 破坏性变更:需在类型/scope后加
    !
    ,或添加
    BREAKING CHANGE:
    页脚(触发MAJOR主版本升级)——仅在正文中说明的破坏性变更无法被工具识别
  • revert
    类型的提交必须在正文中包含
    This reverts commit <hash>.
    ——
    git revert
    会自动生成该内容,请勿删除
  • 绝对不要在提交中添加Claude签名、AI Agent归属信息,或为Claude及其他AI Agent添加
    Co-authored-by
    标记
示例:
feat(auth): add JWT token refresh
fix: prevent race condition on concurrent requests

Introduce request ID and reference to latest request.
Dismiss responses from stale requests.
refactor!: drop support for Go 1.18

BREAKING CHANGE: Go 1.18 no longer supported; uses stdlib APIs from 1.21+

Closing Issues via Commit Messages

通过提交消息自动关闭议题

Both GitHub and GitLab detect keywords in commit messages and automatically close the referenced issue when the commit lands on the default branch. Place the reference in the footer (preferred — keeps the subject line clean).
Keywords:
close
,
closes
,
closed
,
fix
,
fixes
,
fixed
,
resolve
,
resolves
,
resolved
— case-insensitive.
GitHub:
fix(auth): prevent token expiry race condition

Closes #42
Closes owner/repo#99
  • Triggers when merged into the default branch (usually
    main
    )
  • Cross-repo:
    Closes owner/repo#42
  • Close multiple:
    Closes #42, closes #43
  • Works in PR descriptions too
GitLab:
feat: add dark mode support

Resolves #101
Closes group/project#42
  • Triggers when merged into the default branch (configurable per project)
  • Cross-project:
    Closes group/project#42
  • Close multiple:
    Closes #101, closes #102
  • Works in MR descriptions too
Tip: Pair with the commit type —
fix:
closing a bug issue,
feat:
closing a feature request — keeps the changelog semantically coherent.
GitHub和GitLab都会检测提交消息中的关键词,当提交合并到默认分支时,会自动关闭关联的议题。建议将议题引用放在页脚中(可保持主题行简洁)。
支持的关键词:
close
,
closes
,
closed
,
fix
,
fixes
,
fixed
,
resolve
,
resolves
,
resolved
——不区分大小写。
GitHub示例:
fix(auth): prevent token expiry race condition

Closes #42
Closes owner/repo#99
  • 当合并到默认分支(通常为
    main
    )时触发
  • 跨仓库关联:
    Closes owner/repo#42
  • 关闭多个议题:
    Closes #42, closes #43
  • 在PR描述中使用同样有效
GitLab示例:
feat: add dark mode support

Resolves #101
Closes group/project#42
  • 当合并到默认分支(可按项目配置)时触发
  • 跨项目关联:
    Closes group/project#42
  • 关闭多个议题:
    Closes #101, closes #102
  • 在MR描述中使用同样有效
提示: 提交类型与议题类型匹配——用
fix:
关闭Bug议题,用
feat:
关闭功能请求议题——可保持变更日志语义连贯。

Common Mistakes

常见错误

MistakeFix
feat: Added login page
feat: add login page
— imperative, no capital
fix: fix bug.
fix: fix bug
— no trailing period
Subject over 72 charsShorten; move detail to body
Breaking change only in bodyAdd
!
or
BREAKING CHANGE:
footer — tools won't detect body-only
feat(adding-auth): ...
feat(auth): ...
— scope is a noun, not a verb
Closes #42 in subject lineMove to footer — keeps subject clean and parseable
错误示例修正方案
feat: Added login page
feat: add login page
—— 使用祈使语气,首字母不大写
fix: fix bug.
fix: fix bug
—— 末尾无句号
主题行超过72字符缩短主题,将细节移至正文
仅在正文中说明破坏性变更添加
!
BREAKING CHANGE:
页脚——工具无法识别仅在正文中的说明
feat(adding-auth): ...
feat(auth): ...
—— scope为名词而非动词
主题行中包含Closes #42移至页脚——保持主题行简洁可解析

Best Practices

最佳实践

  • Align branch type and commit type —
    feat/auth-*
    branch →
    feat(auth):
    commits
  • One concern per branch — mixing fixes into feature branches obscures the changelog
  • Use scope consistently within a branch —
    feat(auth):
    throughout, not
    feat(user):
    mid-way
  • Squash merge: when squash-merging a PR/MR, the branch commits are collapsed into one — the PR/MR title becomes the commit message. If the title doesn't follow conventional commits format, changelog generation breaks silently. Always set the PR title before squashing.
  • 分支类型与提交类型对齐——
    feat/auth-*
    分支对应
    feat(auth):
    类型的提交
  • 每个分支仅处理一个关注点——在功能分支中混入Bug修复会导致变更日志混乱
  • 在分支中统一使用scope——全程使用
    feat(auth):
    ,不要中途改为
    feat(user):
  • ** squash合并:** 当对PR/MR进行 squash合并时,分支提交会合并为一个——PR/MR标题会成为提交消息。若标题不符合Conventional Commits规范,变更日志生成会静默失败。请务必在squash前设置好PR标题。