git-commit

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git 提交消息

Git Commit Messages

功能概述

Feature Overview

创建清晰、规范的 git 提交消息。优先遵循项目现有的提交历史和格式规范,其次参考 Conventional Commits 标准。确保提交消息简洁、一致且易于理解。
Create clear and standardized git commit messages. Prioritize following the project's existing commit history and format conventions, then refer to the Conventional Commits standard. Ensure commit messages are concise, consistent, and easy to understand.

快速检查流程

Quick Check Process

步骤 1:快速确定项目规范(首次使用)

Step 1: Quickly Determine Project Conventions (First Use)

仅在首次使用或项目规范未知时执行:
bash
undefined
Execute only on first use or when project conventions are unknown:
bash
undefined

查看最近 5 条提交即可

View the last 5 commits only

git log -5 --pretty=format:"%s"

**快速判断**:
- 类型名称:`feat` 或 `feature`、`fix` 或 `bugfix`
- 语言:中文或英文
- Issue 引用:subject 中或 footer 中

**缓存判断结果**,后续提交无需重复分析。
git log -5 --pretty=format:"%s"

**Quick Judgment**:
- Type names: `feat` or `feature`, `fix` or `bugfix`
- Language: Chinese or English
- Issue reference: In subject or footer

**Cache the judgment result**, no need to re-analyze for subsequent commits.

步骤 2:识别变更内容

Step 2: Identify Change Content

bash
undefined
bash
undefined

查看当前变更

View current changes

git diff --stat

判断变更是否属于同一模块:
- 如无不相关内容,直接进入步骤 3
- 如有不相关内容,参考"智能识别变更内容"章节的处理方法
git diff --stat

Judge whether changes belong to the same module:
- If there are no unrelated contents, proceed directly to Step 3
- If there are unrelated contents, refer to the processing method in the "Intelligent Change Content Recognition" section

步骤 3:检查是否需要合并提交

Step 3: Check if Commit Merging is Needed

bash
git status
git log @{u}..HEAD --oneline 2>/dev/null
  • 如无领先提交,直接进入步骤 4
  • 如有领先提交,参考"合并相似提交"章节的判断方法
bash
git status
git log @{u}..HEAD --oneline 2>/dev/null
  • If there are no ahead commits, proceed directly to Step 4
  • If there are ahead commits, refer to the judgment method in the "Merge Similar Commits" section

步骤 4:快速生成提交消息

Step 4: Quickly Generate Commit Message

基于缓存的规范和变更内容快速生成消息,无需详细分析。
Quickly generate messages based on cached conventions and change content, no need for detailed analysis.

优先遵循项目现有规范

Prioritize Following Existing Project Conventions

检查项目提交风格

Check Project Commit Style

bash
undefined
bash
undefined

仅查看最近 5 条提交

View only the last 5 commits

git log -5 --pretty=format:"%s"
undefined
git log -5 --pretty=format:"%s"
undefined

确定项目规范

Determine Project Conventions

  • 类型名称
    feat
    vs
    feature
    fix
    vs
    bugfix
  • Issue 引用:subject 中(
    feat: #123 xxx
    )或 footer 中(
    Closes #123
  • 语言风格:中文或英文
  • 格式宽松度:是否严格遵循某种规范
  • Type names:
    feat
    vs
    feature
    ,
    fix
    vs
    bugfix
  • Issue reference: In subject (
    feat: #123 xxx
    ) or footer (
    Closes #123
    )
  • Language style: Chinese or English
  • Format strictness: Whether strictly following a certain specification

语言选择

Language Selection

统计最近提交,中文占多数用中文,否则用英文。
Count recent commits, use Chinese if most are in Chinese, otherwise use English.

遵循原则

Follow Principles

  • 项目有明确规范 → 严格遵循
  • 项目无明确规范 → 模仿现有风格
  • 项目无明显风格 → 使用 Conventional Commits 标准
  • If the project has clear conventions → strictly follow
  • If the project has no clear conventions → imitate existing style
  • If the project has no obvious style → use Conventional Commits standard

提交消息格式

Commit Message Format

Conventional Commits 标准

Conventional Commits Standard

<type>(<scope>): <subject>

<body>

<footer>
必需部分
  • type
    : 提交类型
  • subject
    : 简短描述(50-72 字符)
可选部分
  • scope
    : 影响范围
  • body
    : 详细描述
  • footer
    : 破坏性变更、引用等
<type>(<scope>): <subject>

<body>

<footer>
Required parts:
  • type
    : Commit type
  • subject
    : Short description (50-72 characters)
Optional parts:
  • scope
    : Impact scope
  • body
    : Detailed description
  • footer
    : Breaking changes, references, etc.

提交类型(type)

Commit Types (type)

优先使用项目类型。无明确类型时参考:
类型说明示例
feat
/
feature
新功能feat: 添加用户认证
fix
/
bugfix
Bug 修复fix: 修复登录超时
docs
文档变更docs: 更新 API 文档
style
代码格式style: 统一代码缩进
refactor
重构refactor: 提取公共方法
perf
性能优化perf: 优化查询性能
test
测试相关test: 添加单元测试
chore
构建/工具链chore: 更新依赖版本
Prioritize using project-specific types. Refer to the following if no clear types are available:
TypeDescriptionExample
feat
/
feature
New featurefeat: add user authentication
fix
/
bugfix
Bug fixfix: resolve login timeout
docs
Documentation changedocs: update API documentation
style
Code formatstyle: unify code indentation
refactor
Refactoringrefactor: extract common methods
perf
Performance optimizationperf: optimize query performance
test
Test-relatedtest: add unit tests
chore
Build/toolchainchore: update dependency versions

Subject 规则

Subject Rules

  • 使用祈使句(现在时态):
    add
    而不是
    added
    adds
  • 首字母小写(英文)或正常大小写(中文)
  • 句尾不加标点
  • 限制在 50-72 个字符
  • 清晰描述做了什么
  • Use imperative mood (present tense):
    add
    instead of
    added
    or
    adds
  • Lowercase first letter (English) or normal case (Chinese)
  • No punctuation at the end
  • Limit to 50-72 characters
  • Clearly describe what was done

Body 规则

Body Rules

  • 解释做了什么以及为什么做
  • 每行限制在 72 个字符
  • 使用祈使句
  • 提供足够的上下文
  • Explain what was done and why
  • Limit each line to 72 characters
  • Use imperative mood
  • Provide sufficient context

Footer 规则

Footer Rules

破坏性变更
BREAKING CHANGE: API endpoints now require authentication
引用 Issue/PR
Closes #123
Refs #456
Breaking changes:
BREAKING CHANGE: API endpoints now require authentication
Reference Issue/PR:
Closes #123
Refs #456

Scope 使用规范

Scope Usage Specifications

Scope 指定提交的影响范围。
常用 Scope
项目类型Scope 示例
Web 应用
api
,
ui
,
auth
,
db
库/框架
core
,
utils
,
types
移动应用
ios
,
android
,
shared
DevOps
ci
,
docker
,
deploy
格式规则
  • 使用小写字母
  • 不超过 10-15 个字符
  • 不确定时省略 scope
Scope specifies the impact range of the commit.
Common Scopes:
Project TypeScope Examples
Web Application
api
,
ui
,
auth
,
db
Library/Framework
core
,
utils
,
types
Mobile Application
ios
,
android
,
shared
DevOps
ci
,
docker
,
deploy
Format Rules:
  • Use lowercase letters
  • No more than 10-15 characters
  • Omit scope if unsure

智能识别变更内容

Intelligent Change Content Recognition

分析原则

Analysis Principles

  1. 识别主要模块:分析所有变更文件,找出大部分内容所属的模块/业务
  2. 识别不搭界内容:找出与主要模块完全不相关的变更
  3. 建议提交策略:主要模块内容提交,不搭界内容暂不提交
  1. Identify main modules: Analyze all changed files to find the module/business most of the content belongs to
  2. Identify unrelated content: Find changes completely unrelated to the main module
  3. Suggest commit strategy: Commit main module content, leave unrelated content for later

判断标准

Judgment Criteria

属于同一模块/业务(建议一起提交)
  • 同一功能的不同部分(API、类型、工具函数)
  • 功能开发 + 相关文档
  • 功能开发 + 相关配置
  • 功能开发 + 依赖更新
完全不搭界(建议暂不提交)
  • 不同业务模块的变更(用户模块 + 订单模块)
  • 代码 + 部署配置文件
  • 代码 + CI/CD 配置
  • 代码 + .gitignore 等配置
Belong to the same module/business (recommend committing together):
  • Different parts of the same feature (API, types, utility functions)
  • Feature development + related documentation
  • Feature development + related configuration
  • Feature development + dependency updates
Completely unrelated (recommend not committing for now):
  • Changes in different business modules (user module + order module)
  • Code + deployment configuration files
  • Code + CI/CD configuration
  • Code + .gitignore and other configurations

处理逻辑

Processing Logic

情况 1:所有变更属于同一模块
直接提交,无需提示。
情况 2:大部分变更属于同一模块,少量不搭界
变更内容:
- src/api/users.ts (用户模块)
- src/types/user.ts (用户模块)
- src/api/orders.ts (订单模块,不搭界)

主要模块:用户模块

不相关变更(建议暂不提交):
- src/api/orders.ts (订单模块,不搭界)

跳过不相关内容,仅提交用户模块相关变更?(y/n)
情况 3:多个模块变更相当
变更内容:
- src/api/users.ts (用户模块,50行)
- src/types/user.ts (用户模块,20行)
- src/api/orders.ts (订单模块,60行)
- src/types/order.ts (订单模块,30行)

用户模块:70行
订单模块:90行

订单模块变更更多,建议提交订单模块相关:
- src/api/orders.ts
- src/types/order.ts

是否仅提交订单模块?(y/n)
或选择提交所有?(all)
Case 1: All changes belong to the same module
Commit directly, no prompt needed.
Case 2: Most changes belong to the same module, with a small amount of unrelated content
Changed content:
- src/api/users.ts (user module)
- src/types/user.ts (user module)
- src/api/orders.ts (order module, unrelated)

Main module: User module

Unrelated changes (recommend not committing for now):
- src/api/orders.ts (order module, unrelated)

Skip unrelated content and only commit user module-related changes? (y/n)
Case 3: Changes in multiple modules are comparable
Changed content:
- src/api/users.ts (user module, 50 lines)
- src/types/user.ts (user module, 20 lines)
- src/api/orders.ts (order module, 60 lines)
- src/types/order.ts (order module, 30 lines)

User module: 70 lines
Order module: 90 lines

Order module has more changes, recommend committing order module-related content:
- src/api/orders.ts
- src/types/order.ts

Commit only order module? (y/n)
Or commit all? (all)

处理方法

Processing Method

bash
undefined
bash
undefined

仅暂存主要模块相关文件

Only stage files related to the main module

git add src/api/users.ts src/types/user.ts git commit -m "feat(api): 添加用户接口"
git add src/api/users.ts src/types/user.ts git commit -m "feat(api): add user interface"

不搭界的文件不暂存,留待后续处理

Unrelated files are not staged, leave for later processing

undefined
undefined

合并相似提交

Merge Similar Commits

前置检查

Pre-check

仅在有领先提交时执行合并判断
bash
undefined
Perform merge judgment only when there are ahead commits:
bash
undefined

检查是否有领先提交

Check if there are ahead commits

AHEAD=$(git rev-list --count @{u}..HEAD 2>/dev/null || echo "0")
if [ "$AHEAD" -eq 0 ]; then

无领先提交,直接创建新提交

echo "本地与远程同步,直接创建新提交" else

有领先提交,进行合并判断

echo "本地有 $AHEAD 个领先提交,检查是否需要合并" fi
undefined
AHEAD=$(git rev-list --count @{u}..HEAD 2>/dev/null || echo "0")
if [ "$AHEAD" -eq 0 ]; then

No ahead commits, create new commit directly

echo "Local is synchronized with remote, create new commit directly" else

There are ahead commits, perform merge judgment

echo "There are $AHEAD ahead commits locally, check if merging is needed" fi
undefined

快速相似度判断

Quick Similarity Judgment

仅需查看上次提交消息和当前变更
bash
undefined
Only need to view the last commit message and current changes:
bash
undefined

获取上次提交

Get the last commit

LAST_COMMIT_MSG=$(git log -1 --pretty=format:"%s")
LAST_COMMIT_MSG=$(git log -1 --pretty=format:"%s")

查看当前变更

View current changes

git diff --stat

**判断标准**:

| 条件 | 操作 |
|------|------|
| 同一类型 + 相同模块文件 | 询问是否合并 |
| 同一 bug 的修复 | 询问是否合并 |
| 文档连续更新 | 询问是否合并 |
| 其他情况 | 不合并 |
git diff --stat

**Judgment Criteria**:

| Condition | Action |
|------|------|
| Same type + same module files | Ask if merging is needed |
| Fix for the same bug | Ask if merging is needed |
| Continuous documentation updates | Ask if merging is needed |
| Other cases | Do not merge |

合并方法

Merge Method

bash
undefined
bash
undefined

暂存当前变更

Stage current changes

git add <files>
git add <files>

合并到上一次提交

Merge into the last commit

git commit --amend --no-edit
git commit --amend --no-edit

或更新提交消息

Or update the commit message

git commit --amend -m "更新后的提交消息"
undefined
git commit --amend -m "Updated commit message"
undefined

实际示例

Practical Examples

基础示例

Basic Examples

英文
feat: add dark mode support
fix: resolve login timeout issue
docs: update README with new installation steps
中文
feat: 添加深色模式支持
fix: 修复登录超时问题
docs: 更新安装指南
English:
feat: add dark mode support
fix: resolve login timeout issue
docs: update README with new installation steps
Chinese:
feat: 添加深色模式支持
fix: 修复登录超时问题
docs: 更新安装指南

带 Scope 的示例

Examples with Scope

feat(auth): implement OAuth2 login
fix(ui): correct button alignment on mobile
refactor(db): optimize user query performance
中文示例
feat(auth): 实现 OAuth2 登录
fix(ui): 修复移动端按钮对齐问题
refactor(db): 优化用户查询性能
feat(auth): implement OAuth2 login
fix(ui): correct button alignment on mobile
refactor(db): optimize user query performance
Chinese Examples:
feat(auth): 实现 OAuth2 登录
fix(ui): 修复移动端按钮对齐问题
refactor(db): 优化用户查询性能

带 Body 的示例

Examples with Body

feat(api): add pagination support

Implement pagination for list endpoints:
- Add `page` and `limit` query parameters
- Return total count and pagination metadata
- Update documentation with examples

Closes #42
fix(auth): resolve token expiration issue

The JWT token expiration was not being checked
correctly, allowing expired tokens to be used.
Added proper validation middleware.

Fixes #128
feat(api): add pagination support

Implement pagination for list endpoints:
- Add `page` and `limit` query parameters
- Return total count and pagination metadata
- Update documentation with examples

Closes #42
fix(auth): resolve token expiration issue

The JWT token expiration was not being checked
correctly, allowing expired tokens to be used.
Added proper validation middleware.

Fixes #128

破坏性变更示例

Breaking Change Examples

feat(api): remove deprecated endpoints

Remove v1 endpoints in favor of v2:
- DELETE /api/v1/users
- DELETE /api/v1/posts

BREAKING CHANGE: v1 endpoints are no longer available.
Please migrate to v2 endpoints.

Migration guide: docs/migration.md
feat(auth): require email verification

All new users must verify their email before
accessing the platform.

BREAKING CHANGE: Email verification is now mandatory.
Existing users are grandfathered in.
feat(api): remove deprecated endpoints

Remove v1 endpoints in favor of v2:
- DELETE /api/v1/users
- DELETE /api/v1/posts

BREAKING CHANGE: v1 endpoints are no longer available.
Please migrate to v2 endpoints.

Migration guide: docs/migration.md
feat(auth): require email verification

All new users must verify their email before
accessing the platform.

BREAKING CHANGE: Email verification is now mandatory.
Existing users are grandfathered in.

常见问题

Common Issues

问题:提交消息太长

Issue: Commit message is too long

解决方案:简化描述,将详细信息放入 body。
feat: add comprehensive user authentication system with OAuth2 support and token management


feat: add user authentication

Implement OAuth2 login and token management system
Solution: Simplify the description and put detailed information in the body.
feat: add comprehensive user authentication system with OAuth2 support and token management


feat: add user authentication

Implement OAuth2 login and token management system

问题:类型选择困难

Issue: Difficulty in selecting type

快速决策
新功能 → feat
修复 bug → fix
重构(功能不变) → refactor
性能优化 → perf
文档 → docs
格式调整 → style
测试 → test
其他 → chore
Quick Decision:
New feature → feat
Bug fix → fix
Refactoring (no functional change) → refactor
Performance optimization → perf
Documentation → docs
Format adjustment → style
Testing → test
Others → chore

问题:Scope 不确定

Issue: Unsure about Scope

指导原则
  • 跨模块变更 → 省略 scope
  • 单一模块变更 → 使用模块名作为 scope
  • 不确定 → 省略 scope
Guidelines:
  • Cross-module changes → omit scope
  • Single module change → use module name as scope
  • Unsure → omit scope

注意事项

Notes

  • 仅本地操作:此技能仅负责创建提交,不执行任何推送
  • 保持原子性:一个提交只做一件事
  • 提交历史清晰:能够重现开发过程
  • 避免敏感信息:不提交密码、密钥、token
  • 遵循项目规范:项目有规范时严格遵循
  • Local operations only: This skill is only responsible for creating commits, no push operations are performed
  • Maintain atomicity: One commit does only one thing
  • Clear commit history: Able to reproduce the development process
  • Avoid sensitive information: Do not commit passwords, keys, tokens
  • Follow project conventions: Strictly follow conventions if the project has them