Skip to content

Commit Commands Git 工作流插件

Commit Commands 插件简化 Git 工作流,提供自动提交、推送和创建 PR 的一站式命令,让开发者专注于编码而非 Git 操作。

概述

Commit Commands 是一个轻量级但实用的 Git 工作流自动化插件。它提供三个核心命令,覆盖日常 Git 操作的主要场景:

命令功能场景
/commit自动生成提交消息并提交日常开发提交
/commit-push-pr创建分支 → 提交 → 推送 → PR完整功能提交流程
/clean_gone清理已删除远程分支的本地分支分支清理维护

设计理念

mermaid
graph LR
    A[分析代码变更] --> B[匹配仓库风格]
    B --> C[生成提交消息]
    C --> D[执行 Git 操作]

    style A fill:#e3f2fd
    style B fill:#e8f5e9
    style C fill:#fff3e0
    style D fill:#f3e5f5

插件的核心设计理念是:

  • 智能分析:理解代码变更的本质
  • 风格匹配:学习仓库的提交消息风格
  • 一键完成:单条命令完成复杂流程

安装与配置

插件安装

bash
# 安装 commit-commands 插件
/plugin install commit-commands@claude-plugins-official

前置要求

  1. Git:已初始化的 Git 仓库
  2. GitHub CLI(可选):用于 /commit-push-pr 创建 PR
bash
# 安装 GitHub CLI(如需创建 PR)
brew install gh  # macOS
gh auth login    # 认证

目录结构

plugins/commit-commands/
├── .claude-plugin/
│   └── plugin.json
├── README.md
└── commands/
    ├── commit.md           # 提交命令
    ├── commit-push-pr.md   # 完整 PR 流程
    └── clean_gone.md       # 分支清理

配置文件 (plugin.json)

json
{
  "name": "commit-commands",
  "description": "Streamline your git workflow with simple commands for committing, pushing, and creating pull requests",
  "version": "1.0.0",
  "author": {
    "name": "Anthropic",
    "email": "support@anthropic.com"
  }
}

核心命令详解

/commit

自动分析变更并生成提交消息,然后提交文件。

语法

bash
/commit

工作流程

mermaid
sequenceDiagram
    participant U as 用户
    participant C as Claude
    participant G as Git

    U->>C: /commit
    C->>G: git status
    G->>C: 变更文件列表
    C->>G: git diff (staged + unstaged)
    G->>C: 变更详情
    C->>G: git branch
    G->>C: 当前分支
    C->>G: git log -10
    G->>C: 最近 10 条提交

    C->>C: 分析变更 + 匹配风格
    C->>G: git add + git commit
    G->>C: 提交成功

    C->>U: 显示提交结果

允许的工具

yaml
allowed-tools:
  - Bash(git add:*)
  - Bash(git status:*)
  - Bash(git commit:*)

智能特性

特性说明
变更分析理解代码变更的类型(新功能、修复、重构等)
风格匹配学习仓库最近 10 条提交的消息风格
安全过滤自动避免提交敏感文件(.env 等)
单次提交所有变更作为单个提交

提交消息风格示例

bash
# 如果仓库使用 Conventional Commits
feat: add user authentication module
fix: resolve null pointer in login flow

# 如果仓库使用简单描述
Add user authentication
Fix login bug

# 如果仓库使用详细描述
Add user authentication module

- Implement JWT token handling
- Add login/logout endpoints
- Create AuthService class

/commit-push-pr

完整的 PR 工作流:创建分支 → 提交 → 推送 → 创建 PR。

语法

bash
/commit-push-pr

工作流程

mermaid
graph TD
    A[/commit-push-pr] --> B[获取 git status]
    B --> C[获取 git diff]
    C --> D{在 main 分支?}

    D -->|是| E[创建新分支]
    D -->|否| F[使用当前分支]

    E --> G[创建提交]
    F --> G

    G --> H[推送到 origin]
    H --> I[创建 PR]
    I --> J[显示 PR 链接]

    style A fill:#e3f2fd
    style J fill:#c8e6c9

允许的工具

yaml
allowed-tools:
  - Bash(git checkout --branch:*)
  - Bash(git add:*)
  - Bash(git status:*)
  - Bash(git push:*)
  - Bash(git commit:*)
  - Bash(gh pr create:*)

完整流程(单条消息完成):

  1. 获取 git status 和 diff
  2. 如果在 main 分支,创建新分支
  3. 创建单个 commit(带有合适的消息)
  4. 推送分支到 origin
  5. 使用 gh pr create 创建 PR
  6. 返回 PR 链接

示例输出

✅ Workflow completed!

Branch: feature/user-auth
Commit: feat: add user authentication module

PR Created: https://github.com/org/repo/pull/42

PR Title: Add user authentication module
PR Description:
- Implement JWT token handling
- Add login/logout endpoints
- Create AuthService class

/clean_gone

清理所有标记为 [gone] 的本地分支(远程已删除但本地存在)。

语法

bash
/clean_gone

工作流程

mermaid
graph TD
    A[/clean_gone] --> B[git branch -v]
    B --> C[识别 gone 分支]
    C --> D[git worktree list]
    D --> E[识别相关 worktrees]

    E --> F{有 gone 分支?}
    F -->|否| G[报告: 无需清理]
    F -->|是| H[逐个处理]

    H --> I[移除 worktree]
    I --> J[删除分支]
    J --> K[报告结果]

    style A fill:#e3f2fd
    style G fill:#c8e6c9
    style K fill:#c8e6c9

什么是 [gone] 分支?

当远程分支被删除(如 PR 合并后)但本地分支仍然存在时,Git 会将其标记为 [gone]

bash
$ git branch -v
* main                 abc1234 Latest commit
  feature/old-feature  def5678 [gone] Old feature
  feature/merged-pr    ghi9012 [gone] Merged PR

清理内容

类型操作
Worktreegit worktree remove
分支git branch -D

示例输出

🧹 Cleaning gone branches...

Found 3 gone branches:
- feature/old-feature
- feature/merged-pr
- bugfix/resolved-issue

Removing worktree: /path/to/worktree/old-feature
Deleting branch: feature/old-feature ✓

Removing worktree: /path/to/worktree/merged-pr
Deleting branch: feature/merged-pr ✓

Deleting branch: bugfix/resolved-issue ✓

✅ Cleaned 3 branches

使用场景

场景 1:日常功能开发

bash
# 编写代码...

# 一键提交
/commit

# 输出示例
# ✅ Created commit: feat: add user profile page
# Files: src/pages/Profile.tsx, src/api/user.ts

场景 2:完成功能并创建 PR

bash
# 完成功能开发...

# 一键完成提交 + 推送 + PR
/commit-push-pr

# 输出示例
# ✅ Created PR: https://github.com/org/repo/pull/42

场景 3:定期清理分支

bash
# 项目维护时运行
/clean_gone

# 输出示例
# ✅ Cleaned 5 gone branches

场景 4:快速修复

bash
# 修复了一个 bug...

# 如果只需要提交(不创建 PR)
/commit

# 如果需要立即提交 PR
/commit-push-pr

最佳实践

提交消息质量

插件会自动匹配仓库风格,但你可以通过以下方式提高质量:

实践说明
保持一致仓库中使用统一的提交风格
原子提交每次提交只包含一个逻辑变更
有意义的历史最近的提交消息有参考价值

推荐的工作流

mermaid
graph LR
    A[开发] --> B[/commit]
    B --> C{需要 PR?}
    C -->|是| D[/commit-push-pr]
    C -->|否| E[继续开发]

    D --> F[代码审查]
    F --> G[合并]
    G --> H[/clean_gone]

    style A fill:#e3f2fd
    style H fill:#c8e6c9

安全注意事项

注意事项说明
敏感文件插件会自动跳过 .env 等文件
大文件注意不要提交大型二进制文件
分支保护确保 main 分支有适当的保护规则

与其他插件配合

与 code-review 配合

bash
# 1. 创建 PR
/commit-push-pr

# 2. 自动代码审查
/code-review --comment

# 3. 修复问题后重新提交
/commit

与 feature-dev 配合

bash
# 1. 使用 feature-dev 开发功能
/feature-dev "添加用户认证"

# 2. 开发完成后提交
/commit-push-pr

# 3. 清理合并后的分支
/clean_gone

常见问题

Q: 如何自定义提交消息?

A: 目前插件自动生成消息。如需自定义,可以使用标准 Git 命令:

bash
git commit -m "your custom message"

Q: 为什么有些文件没有被提交?

A: 插件会自动跳过:

  • .env 及相关环境文件
  • .gitignore 中排除的文件
  • 大型二进制文件

Q: /commit-push-pr 失败怎么办?

A: 检查:

  1. 是否已安装并认证 gh CLI
  2. 是否有仓库的推送权限
  3. 远程仓库是否配置正确

Q: 如何撤销刚才的提交?

A: 使用标准 Git 命令:

bash
# 撤销提交但保留更改
git reset --soft HEAD~1

# 完全撤销提交和更改
git reset --hard HEAD~1

总结

Commit Commands 插件提供了三个简洁有力的 Git 工作流命令:

命令用途一句话说明
/commit智能提交自动生成消息并提交
/commit-push-pr完整 PR 流程分支→提交→推送→PR
/clean_gone分支清理删除远程已删除的本地分支

这三个命令覆盖了日常 Git 工作流的主要场景,让开发者可以专注于编码本身。


参考资源


本文档基于 commit-commands 插件 v1.0.0 编写

基于 MIT 许可证发布。内容版权归作者所有。