git-commit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit 提交消息
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
undefinedExecute 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
undefinedbash
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
undefinedbash
undefined仅查看最近 5 条提交
View only the last 5 commits
git log -5 --pretty=format:"%s"
undefinedgit log -5 --pretty=format:"%s"
undefined确定项目规范
Determine Project Conventions
- 类型名称:vs
feat、featurevsfixbugfix - Issue 引用:subject 中()或 footer 中(
feat: #123 xxx)Closes #123 - 语言风格:中文或英文
- 格式宽松度:是否严格遵循某种规范
- Type names: vs
feat,featurevsfixbugfix - Issue reference: In subject () or footer (
feat: #123 xxx)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 - : 简短描述(50-72 字符)
subject
可选部分:
- : 影响范围
scope - : 详细描述
body - : 破坏性变更、引用等
footer
<type>(<scope>): <subject>
<body>
<footer>Required parts:
- : Commit type
type - : Short description (50-72 characters)
subject
Optional parts:
- : Impact scope
scope - : Detailed description
body - : Breaking changes, references, etc.
footer
提交类型(type)
Commit Types (type)
优先使用项目类型。无明确类型时参考:
| 类型 | 说明 | 示例 |
|---|---|---|
| 新功能 | feat: 添加用户认证 |
| Bug 修复 | fix: 修复登录超时 |
| 文档变更 | docs: 更新 API 文档 |
| 代码格式 | style: 统一代码缩进 |
| 重构 | refactor: 提取公共方法 |
| 性能优化 | perf: 优化查询性能 |
| 测试相关 | test: 添加单元测试 |
| 构建/工具链 | chore: 更新依赖版本 |
Prioritize using project-specific types. Refer to the following if no clear types are available:
| Type | Description | Example |
|---|---|---|
| New feature | feat: add user authentication |
| Bug fix | fix: resolve login timeout |
| Documentation change | docs: update API documentation |
| Code format | style: unify code indentation |
| Refactoring | refactor: extract common methods |
| Performance optimization | perf: optimize query performance |
| Test-related | test: add unit tests |
| Build/toolchain | chore: update dependency versions |
Subject 规则
Subject Rules
- 使用祈使句(现在时态):而不是
add或addedadds - 首字母小写(英文)或正常大小写(中文)
- 句尾不加标点
- 限制在 50-72 个字符
- 清晰描述做了什么
- Use imperative mood (present tense): instead of
addoraddedadds - 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 #456Breaking changes:
BREAKING CHANGE: API endpoints now require authenticationReference Issue/PR:
Closes #123
Refs #456Scope 使用规范
Scope Usage Specifications
Scope 指定提交的影响范围。
常用 Scope:
| 项目类型 | Scope 示例 |
|---|---|
| Web 应用 | |
| 库/框架 | |
| 移动应用 | |
| DevOps | |
格式规则:
- 使用小写字母
- 不超过 10-15 个字符
- 不确定时省略 scope
Scope specifies the impact range of the commit.
Common Scopes:
| Project Type | Scope Examples |
|---|---|
| Web Application | |
| Library/Framework | |
| Mobile Application | |
| DevOps | |
Format Rules:
- Use lowercase letters
- No more than 10-15 characters
- Omit scope if unsure
智能识别变更内容
Intelligent Change Content Recognition
分析原则
Analysis Principles
- 识别主要模块:分析所有变更文件,找出大部分内容所属的模块/业务
- 识别不搭界内容:找出与主要模块完全不相关的变更
- 建议提交策略:主要模块内容提交,不搭界内容暂不提交
- Identify main modules: Analyze all changed files to find the module/business most of the content belongs to
- Identify unrelated content: Find changes completely unrelated to the main module
- 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
undefinedbash
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
undefinedundefined合并相似提交
Merge Similar Commits
前置检查
Pre-check
仅在有领先提交时执行合并判断:
bash
undefinedPerform 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
undefinedAHEAD=$(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
undefinedOnly 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
undefinedbash
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 "更新后的提交消息"
undefinedgit 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 stepsChinese:
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 performanceChinese 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 #42fix(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 #128feat(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 #42fix(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.mdfeat(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.mdfeat(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 systemSolution: 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
其他 → choreQuick 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