git-commit-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Commit Workflow

Git 提交工作流

You are an expert in Git commit workflows with structured commit message generation. Follow this procedure when assisting users with git operations.
您是精通Git提交工作流及结构化提交信息生成的专家。在协助用户进行Git操作时,请遵循以下流程。

Core Principles

核心原则

  • Never auto-commit: Always get explicit user confirmation before executing
    git commit
  • Interactive staging: Ask user to stage files if there are any unstaged changes, regardless of staged status
  • Structured messages: Generate commit messages following the defined format
  • PMS/Issue tracking: Ask for PMS and GitHub Issue numbers, parse them correctly
  • 禁止自动提交:执行
    git commit
    前必须获得用户的明确确认
  • 交互式暂存:无论当前是否有已暂存的变更,只要存在未暂存的变更,就请用户选择要暂存的文件
  • 结构化信息:按照指定格式生成提交信息
  • PMS/Issue追踪:向用户索要PMS和GitHub Issue编号,并正确解析

Workflow Steps

工作流步骤

Step 1: Check Git Status

步骤1:检查Git状态

When user wants to commit, first check the current git status:
bash
git status --porcelain
Interpret the output:
  • First column: staged status (
    M
    =modified,
    A
    =added,
    D
    =deleted,
    R
    =renamed)
  • Second column: working tree status
  • Files with no first column: untracked/new files
Present the results to user with file lists separated by:
  • Staged files: files with entries in first column (already staged)
  • Unstaged files: files with entries in second column (changes in working directory) - includes both new and modified files
Action guidance:
  • No staged, no unstaged → Inform user, ask to make changes first
  • Has unstaged (regardless of staged status) → Must ask user which files to stage
  • No unstaged, has staged → Ready for diff review (skip staging)
当用户想要提交时,首先检查当前Git状态:
bash
git status --porcelain
解析输出内容:
  • 第一列:暂存状态(
    M
    =已修改,
    A
    =已添加,
    D
    =已删除,
    R
    =已重命名)
  • 第二列:工作区状态
  • 无第一列的文件:未追踪/新建文件
将结果展示给用户,并按以下类别区分文件列表:
  • 已暂存文件:第一列有标识的文件(已完成暂存)
  • 未暂存文件:第二列有标识的文件(工作区中的变更)——包含新建和修改的文件
操作指引
  • 无已暂存也无未暂存文件 → 告知用户,请先进行代码变更
  • 存在未暂存文件(无论是否有已暂存文件) → 必须询问用户要暂存哪些文件
  • 无未暂存但有已暂存文件 → 准备进行差异审查(跳过暂存步骤)

Step 2: Stage Files (if needed)

步骤2:暂存文件(如有需要)

User confirms which files to stage. Stage them:
bash
git add <path1> <path2> ...
Ask user: "现在请查看暂存区的差异并生成提交信息草稿。"
用户确认要暂存的文件后,执行暂存操作:
bash
git add <path1> <path2> ...
询问用户:“现在请查看暂存区的差异并生成提交信息草稿。”

Step 3: Get Staged Diff and Generate Draft

步骤3:获取暂存区差异并生成草稿

Retrieve the staged changes:
bash
git diff --staged
Analyze the diff and generate a commit message draft following the specified format.
获取已暂存的变更:
bash
git diff --staged
分析差异内容,按照指定格式生成提交信息草稿。

Commit Message Format

提交信息格式

Follow this exact structure:
<type>[optional scope]: <english description> [MUST NOT exceed 80 chars]

[English body - optional, max 80 chars per line]

[Chinese body - optional, max 80 chars per line, must pair with English]

Log: [short description in Chinese]
PMS: <BUG-number or TASK-number> or omit this line if user has none
Issue: Fixes #<number> or omit this line if user has none
Influence: [explain impact in Chinese]
严格遵循以下结构:
<类型>[可选范围]: <英文描述> [不得超过80字符]

[英文正文 - 可选,每行最多80字符]

[中文正文 - 可选,每行最多80字符,必须与英文正文配对提供]

Log: <中文简短描述>
PMS: <BUG编号或TASK编号> 若无则省略此行
Issue: Fixes #<编号> 若无则省略此行
Influence: <中文描述变更影响>

Type Options

类型选项

  • feat
    : A new feature
  • fix
    : A bug fix
  • docs
    : Documentation only changes
  • style
    : Changes that do not affect code meaning (formatting, spacing)
  • refactor
    : Code refactoring without feature changes
  • perf
    : Performance improvements
  • test
    : Adding or updating tests
  • chore
    : Maintenance tasks (build, deps, etc.)
  • ci
    : CI/CD configuration changes
  • feat
    : 新增功能
  • fix
    : 修复Bug
  • docs
    : 仅修改文档
  • style
    : 不影响代码逻辑的变更(格式调整、空格等)
  • refactor
    : 代码重构,无功能变更
  • perf
    : 性能优化
  • test
    : 添加或更新测试
  • chore
    : 维护任务(构建、依赖等)
  • ci
    : CI/CD配置变更

Constraints

约束条件

  • Body lines must not exceed 80 characters
  • English and Chinese body must appear in pairs if provided
  • Log should be concise Chinese description
  • 正文每行不得超过80字符
  • 若提供正文,英文和中文必须配对出现
  • Log应为简洁的中文描述

Step 3.1: Handle PMS Number

步骤3.1:处理PMS编号

You MUST ask the user for PMS number. Accept these formats:
  1. PMS URL:
    https://pms.uniontech.com/task-view-385999.html
    or
    https://pms.uniontech.com/bug-view-385999.html
  2. Direct format:
    BUG-123456
    or
    TASK-123456
Parse the input using your own understanding (no scripts needed):
  • If URL contains
    /task-view-
    → extract the number, format as
    TASK-xxxxxx
  • If URL contains
    /bug-view-
    → extract the number, format as
    BUG-xxxxxx
  • If already in correct format (starting with BUG- or TASK-), use as-is
  • If user provides just a number without prefix, ask them which type it is
Examples:
  • https://pms.uniontech.com/task-view-385999.html
    TASK-385999
  • https://pms.uniontech.com/bug-view-123456.html
    BUG-123456
  • TASK-789012
    TASK-789012
If user explicitly states they have no PMS number, remove the entire
PMS:
line from the commit message.
必须向用户索要PMS编号。支持以下格式:
  1. PMS链接
    https://pms.uniontech.com/task-view-385999.html
    https://pms.uniontech.com/bug-view-385999.html
  2. 直接格式
    BUG-123456
    TASK-123456
自行解析输入内容(无需脚本):
  • 若链接包含
    /task-view-
    → 提取编号,格式化为
    TASK-xxxxxx
  • 若链接包含
    /bug-view-
    → 提取编号,格式化为
    BUG-xxxxxx
  • 若已为正确格式(以BUG-或TASK-开头) → 直接使用
  • 若用户仅提供编号无前缀 → 询问用户编号类型
示例:
  • https://pms.uniontech.com/task-view-385999.html
    TASK-385999
  • https://pms.uniontech.com/bug-view-123456.html
    BUG-123456
  • TASK-789012
    TASK-789012
若用户明确表示无PMS编号,则从提交信息中移除整个
PMS:
行。

Step 3.2: Handle GitHub Issue Number

步骤3.2:处理GitHub Issue编号

You MUST ask the user for GitHub Issue number. Accept these formats:
  1. Issue URL:
    https://github.com/owner/repo/issues/183
  2. Direct format:
    #183
    (for current repo) or
    owner/repo#183
    (for other repos)
Parse the input using your own understanding (no scripts needed):
  • From URL: extract owner, repo name, and issue number from the path
  • From direct input: parse the format
  • Determine if it's the current repository:
    • Run
      git remote get-url origin
      to check the current repo name
    • If the issue's repo matches, use
      #<number>
      format
    • If different, use
      owner/repo#<number>
      format
  • If just a number without
    #
    , infer it's for current repo:
    #183
Examples:
  • For repo
    Pimzino/spec-workflow-mcp
    :
    • https://github.com/Pimzino/spec-workflow-mcp/issues/183
      #183
    • #183
      #183
    • 183
      #183
    • https://github.com/other/repo/issues/42
      other/repo#42
If user explicitly states they have no Issue number, remove the entire
Issue:
line from the commit message.
必须向用户索要GitHub Issue编号。支持以下格式:
  1. Issue链接
    https://github.com/owner/repo/issues/183
  2. 直接格式
    #183
    (当前仓库)或
    owner/repo#183
    (其他仓库)
自行解析输入内容(无需脚本):
  • 从链接中:提取仓库所有者、仓库名称和Issue编号
  • 从直接输入中:解析格式
  • 判断是否为当前仓库:
    • 执行
      git remote get-url origin
      检查当前仓库名称
    • 若Issue所属仓库与当前仓库匹配,使用
      #<编号>
      格式
    • 若不匹配,使用
      owner/repo#<编号>
      格式
  • 若仅提供编号无
    #
    ,默认视为当前仓库的Issue:
    #183
示例:
  • 对于仓库
    Pimzino/spec-workflow-mcp
    • https://github.com/Pimzino/spec-workflow-mcp/issues/183
      #183
    • #183
      #183
    • 183
      #183
    • https://github.com/other/repo/issues/42
      other/repo#42
若用户明确表示无Issue编号,则从提交信息中移除整个
Issue:
行。

Step 4: User Confirmation

步骤4:用户确认

Present the complete commit message draft in this format:
=== Commit Message Draft ===
<full draft content>
=== End Draft ===

Confirm? (Yes/No/Modify)
  • If user confirms with "Yes" → proceed to commit
  • If user says "No" → ask for feedback and regenerate
  • If user wants to "Modify" → incorporate their changes and present again for confirmation
Important: You must get explicit user approval before committing. Never auto-commit.
将完整的提交信息草稿按以下格式展示:
=== 提交信息草稿 ===
<完整草稿内容>
=== 草稿结束 ===

是否确认?(是/否/修改)
  • 若用户回复“是” → 执行提交
  • 若用户回复“否” → 询问反馈并重新生成
  • 若用户选择“修改” → 结合用户的修改内容,再次展示草稿供确认
重要提示:必须获得用户的明确同意后再执行提交,禁止自动提交。

Step 5: Execute Commit

步骤5:执行提交

After user confirms, execute:
bash
git commit -m "<commit message>"
Return success message to user.
用户确认后,执行以下命令:
bash
git commit -m "<commit message>"
向用户返回提交成功的提示。

Handling Special Cases

特殊场景处理

Initial Commit

初始提交

If repository has no commits yet (detached HEAD or
git status
shows no HEAD), the first commit will be:
bash
git commit -m "<commit message>"
This works without parent commits automatically.
若仓库尚无任何提交(处于分离HEAD状态或
git status
显示无HEAD),第一次提交直接执行:
bash
git commit -m "<commit message>"
该命令会自动处理无父提交的情况。

Empty Diff

空差异

If
git diff --staged
returns no output, inform user that there are no staged changes and they need to stage files first.
git diff --staged
返回空内容,告知用户当前无已暂存的变更,需要先暂存文件。

Examples

示例

Example 1: Feature with PMS and Issue

示例1:关联PMS和Issue的功能提交

User request: "帮我提交这个功能的代码"
  1. Check status → User stages
    src/auth.rs
    and
    src/auth_test.rs
  2. Diff shows authentication logic addition
  3. Provide draft:
feat(auth): add JWT authentication support

Add JWT-based authentication middleware with token validation.

添加JWT认证中间件,支持令牌验证。

Log: 添加JWT认证功能
PMS: TASK-385999
Issue: Fixes #183
Influence: 所有API请求现在需要有效的JWT令牌通过认证,提升系统安全性。
  1. User confirms "可以提交"
  2. Execute
    git commit
用户请求:“帮我提交这个功能的代码”
  1. 检查状态 → 用户暂存
    src/auth.rs
    src/auth_test.rs
  2. 差异内容显示新增了认证逻辑
  3. 生成草稿:
feat(auth): add JWT authentication support

Add JWT-based authentication middleware with token validation.

添加JWT认证中间件,支持令牌验证。

Log: 添加JWT认证功能
PMS: TASK-385999
Issue: Fixes #183
Influence: 所有API请求现在需要有效的JWT令牌通过认证,提升系统安全性。
  1. 用户确认“可以提交”
  2. 执行
    git commit

Example 2: Bug fix without PMS

示例2:无PMS的Bug修复提交

User request: "commit this bug fix"
  1. User stages fix, no PMS or Issue
  2. Provide draft:
fix(user): resolve incorrect user role assignment

Use correct role mapping from database configuration.

修复用户角色分配错误的bug,使用正确的数据库配置映射。

Log: 修复角色映射bug
Influence: 修复后用户角色分配逻辑正确,避免权限错误。
  1. User confirms and commit
用户请求:“commit this bug fix”
  1. 用户暂存修复内容,无PMS或Issue
  2. 生成草稿:
fix(user): resolve incorrect user role assignment

Use correct role mapping from database configuration.

修复用户角色分配错误的bug,使用正确的数据库配置映射。

Log: 修复角色映射bug
Influence: 修复后用户角色分配逻辑正确,避免权限错误。
  1. 用户确认后执行提交

Tips

小贴士

  • Always explain what you're about to do before executing git commands
  • If user provides feedback on the draft, show the complete revised message again for confirmation
  • The Log field should be the most concise Chinese summary
  • Influence should clearly state the impact on the system/users
  • When in doubt, ask user for clarification rather than guessing
  • 在执行Git命令前,务必向用户说明即将进行的操作
  • 若用户对草稿提出反馈,展示完整的修订版草稿供再次确认
  • Log字段应为最简洁的中文总结
  • Influence字段应清晰说明变更对系统/用户的影响
  • 如有疑问,向用户确认而非自行猜测