mcpField
C
commit-splitter

commit-splitter

github.com/sundial-org开发15365 次安装15 周前更新
代码仓库

关于 commit-splitter

commit-splitter 是一项将工作区改动按编号 diff 块拆分为多个 Git 提交的技能,适合需要按明确计划提交改动的开发者。它通过提取指定块并暂存补丁来保留原始改动。

主要功能

  1. 01为 diff 块编号
  2. 02按块规划提交
  3. 03用补丁暂存指定改动
  4. 04提交后核对暂存内容

适用场景

  1. 01将无关改动分成多个提交
  2. 02执行已确认的分块提交计划
  3. 03处理暂存或提交过程中的错误

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sundial-org/skills --skill commit-splitter

安装后在 Claude Code 中输入/commit-splitter确认已加载。

附带文件scripts/extract-hunks.py

详细介绍

结构化 Git 提交

工作流

1. 安全暂存
git stash push -m "save before structured commit: <brief description>" --include-untracked
git stash apply

始终保留已应用的更改。不要在各次提交之间重新暂存。只在最后一步删除安全暂存。

2. 检查并为代码块编号

为所有已修改的文件生成补丁,并列出其中的代码块。 路径包含特殊字符(方括号、空格等)时,请加引号:

git diff "<file>" > /tmp/<file>.patch
.claude/skills/commit-splitter/scripts/extract-hunks.py /tmp/<file>.patch --list

这会输出带编号的代码块:

Hunks (3 total):
  1: @@ -10,6 +10,7 @@ def foo():  (+1/-0) print("perf log")...
  2: @@ -25,4 +26,6 @@ def bar():  (+2/-0) import threading...
  3: @@ -50,3 +53,4 @@ def baz():  (+1/-0) min_containers=1...
3. 按代码块编号提出提交计划

在计划中按编号引用代码块:

modal_app.py:
  Hunk 1: perf logging
  Hunk 2: async threading
  Hunk 3: min_containers=1

Commit 1: "perf: add logging" - modal_app.py hunks 1
Commit 2: "perf: async + min_containers" - modal_app.py hunks 2,3
4. 执行提交

重要:严格遵循已批准的计划。 绝不要因为“拆分起来似乎很难”而合并提交。

绝不要使用以下捷径:

  • 使用 git checkout 重置文件,然后重新编辑
  • 直接使用文件编辑(Edit/Update 工具)修改源文件
  • 因为更改“交错在一起”而合并提交
  • 使用 Write 工具或 heredoc 创建补丁

整个文件: 直接执行 git add <file>

新文件: 直接执行 git add <file>

部分文件(指定代码块):

.claude/skills/commit-splitter/scripts/extract-hunks.py /tmp/<file>.patch <hunk_numbers> > /tmp/commit.patch
git apply --cached /tmp/commit.patch

示例:

# Commit 1: just perf logging (hunk 1)
.claude/skills/commit-splitter/scripts/extract-hunks.py /tmp/modal_app.patch 1 > /tmp/commit1.patch
git apply --cached /tmp/commit1.patch
git commit -m "perf: add logging"

# Commit 2: async + min_containers (hunks 2,3)
.claude/skills/commit-splitter/scripts/extract-hunks.py /tmp/modal_app.patch 2,3 > /tmp/commit2.patch
git apply --cached /tmp/commit2.patch
git commit -m "perf: async and min_containers"

每次提交后,使用 git diff --cached --stat 验证。

5. extract-hunks.py 参考

位置:.claude/skills/commit-splitter/scripts/extract-hunks.py

# List hunks with summaries
.claude/skills/commit-splitter/scripts/extract-hunks.py file.patch --list

# Extract specific hunks
.claude/skills/commit-splitter/scripts/extract-hunks.py file.patch 1,3,5      # hunks 1, 3, 5
.claude/skills/commit-splitter/scripts/extract-hunks.py file.patch 2-4        # hunks 2, 3, 4
.claude/skills/commit-splitter/scripts/extract-hunks.py file.patch 1,3-5,7    # mixed

该脚本会将有效补丁输出到标准输出。将其重定向到文件,然后执行 git apply --cached。

恢复

如果出现问题:

  • 执行 git reset HEAD 取消暂存
  • 执行 git stash list 查找安全暂存
  • 执行 git checkout -- <file>,然后执行 git stash apply 还原文件

所有提交成功后:

  • 执行 git stash drop 删除安全暂存

评论

支持 Markdown