Command Palette

Search for a command to run...

Back to skills
skill № 42009

commit-own-changes

基于 git-hunk 提交改动,防止带入其他无关的未提交修改。事关安全,在执行任何 `git add`,`git commit` 命令前 **必须** 使用本技能。例外:若你当前在单独 worktree 中工作,则无需使用本技能。

FreeRuns bundled scripts
john-walks-slowby john-walks-slow★ 30 GitHub stars

Install commit-own-changes in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

commit-own-changes

基于 git-hunk 精准选择提交范围,防止带入其他无关的未提交修改。

依赖

本技能依赖 git-hunk(按 hunk 精选提交范围)。

安装:

pip install git-hunk

安装后 git-hunk 命令可用(git-hunk list / git-hunk show <id> / git-hunk stage <id>)。详见 https://github.com/wkentaro/git-hunk。

工作流程

  1. 获取锁

防止同一时间其他 Agent 进行 Git 操作:

{skill_dir}/scripts/git-lock.sh acquire <task-id> /path/to/repo   # Linux/macOS:显式传仓库路径(或先 cd 进仓库)
{skill_dir}\scripts\git-lock.ps1 -Action acquire -TaskId <task-id> -RepoDir <repo>   # Windows

<task-id> 建议用本次任务的极简英文 summary,如 fix-login-crash、add-dark-mode。获取锁和释放锁时必须使用同一 ID。

重要: acquire 会自动等待直到上一个锁释放;请不要设置超时时间,耐心等待。禁止手动检查/删除锁文件。禁止跳过本步骤进行后续操作。 重要: 必须在目标仓库内调用(或显式传仓库路径)——脚本按调用者 cwd 解析 .git,从非仓库目录调用会把锁写到错误位置(2026-09-25 实战:曾静默在文件系统根创建 /agent.lock,导致目标仓库互斥完全失效且遗留 5 分钟死锁)。 若 acquire 执行失败(输出不包含 acquired),立即中止提交,向用户报告异常。

  1. 列举修改状态

确认有哪些 hunk 是需要提交的:

git status             # 确认仓库状态
git-hunk list          # 全部 hunk
git-hunk list <files...>   # 或只看特定文件的 hunk

可以使用 show 确认 hunk 的内容:

git-hunk show <hunk_ids...>
  1. 清除暂存区

防止过去残留的暂存项干扰本次提交:

git restore --staged .
  1. 暂存

如果文件的所有改动都属于本轮提交,直接全量暂存:

git add <files...>

否则用 git-hunk stage 精确选择:

git-hunk stage <hunk_ids...>

与其他 Agent 修改重叠时的处理(混在同一行,无法拆分):

  • Case 1 — 要提交 B,但 B 已基于 A 的未提交修改:向用户说明情况,询问是否需要等待前置修改提交后再提交本修改。如用户要求等待,则恢复暂存区并释放锁。如用户同意直接一并提交,则提交 A+B 整体。在 message 中备注包含 A 的改动。
  • Case 2 — 要提交 A,但 A 之后又叠加了 B 的修改:构造 patch 仅含 A 的部分,用 git apply --cached 暂存;B 留在工作区,留待 B 的 Agent 提交。

构造 patch 的流程与注意事项:

  1. 优先 git-hunk stage / git add -p 选 hunk——若能独立成 hunk,不需要 patch。
  2. 若必须手写 patch(A、B 混在同一行无法用 hunk 表达),写完后必须转 LF 并验证:
$c = [System.IO.File]::ReadAllText('x.patch'); $c = $c -replace "`r`n", "`n"; [System.IO.File]::WriteAllText('x.patch', $c, (New-Object System.Text.UTF8Encoding($false)))
git apply --cached --check x.patch
  1. 提交

注意:PowerShell 不支持 heredoc,提交消息必须使用 -F - 指定

"<type>: <title>`n`n- <file>: <changes>`n- <file>: <changes>" | git commit -F -
  1. 释放锁
{skill_dir}/scripts/git-lock.sh release <task-id> /path/to/repo   # Linux/macOS:与 acquire 使用同一仓库路径
{skill_dir}\scripts\git-lock.ps1 -Action release -TaskId <task-id> -RepoDir <repo>   # Windows

原则

  • 同时有多个 Agent 在工作。请勿假设只有你自己修改过某一文件。
  • 禁止 git add -A。
  • 在适用时,建议在一行脚本内完成第 3~6 步以提高效率。例:
git restore --staged . && git add foo.md && git-hunk stage a1b2c3 && "<type>: <title>`n`n- <file>: <changes>`n- <file>: <changes>" | git commit -F - && {skill_dir}\scripts\git-lock.ps1 -Action release -TaskId <task-id>
Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

  • scripts/git-lock.bat
  • scripts/git-lock.ps1
  • scripts/git-lock.sh

FAQ

What does the commit-own-changes skill do?

基于 git-hunk 提交改动,防止带入其他无关的未提交修改。事关安全,在执行任何 `git add`,`git commit` 命令前 **必须** 使用本技能。例外:若你当前在单独 worktree 中工作,则无需使用本技能。

How do I install the commit-own-changes skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the commit-own-changes skill run scripts?

Yes, this skill bundles executable scripts. Review the source before installing.

Related skills

Compare commit-own-changes with