git-hunk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Hunk
Git Hunk
Use to inspect, stage, unstage, and commit precise text changes without interactive prompts.
git-hunkPrefer over raw line ranges when you need selectors that survive unrelated rescans. Keep for safety, treat as snapshot-local, and treat raw line ranges as a last resort.
change_keysnapshot_idchange_id使用无需交互式提示即可检查、暂存、取消暂存和提交精确的文本变更。
git-hunk当你需要能在无关重扫描中保留的选择器时,优先使用而非原始行范围。务必保留以确保安全,将视为快照本地标识,仅在万不得已时使用原始行范围。
change_keysnapshot_idchange_idQuick Start
快速开始
- Scan the repo and capture a snapshot:
bash
git-hunk scan --mode stage --json- Inspect a selectable unit:
bash
git-hunk show --mode stage <hunk-id>
git-hunk show --mode stage <change-id> --json
git-hunk show --mode stage <change-key> --json- Apply a selection:
bash
git-hunk stage --snapshot <snapshot-id> --hunk <hunk-id>
git-hunk stage --snapshot <snapshot-id> --change <change-id>
git-hunk stage --snapshot <snapshot-id> --change-key <change-key>
git-hunk stage --snapshot <snapshot-id> --hunk <hunk-id>:new:41-44
git-hunk stage --snapshot <snapshot-id> --change-key <change-key> --dry-run --json- Commit the exact selection directly:
bash
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change <change-id>
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change-key <change-key>
git-hunk commit -m "fix: message" --snapshot <snapshot-id> --hunk <hunk-id>:old:18-22
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change-key <change-key> --dry-run --json- Resolve a file+line hint into durable selectors:
bash
git-hunk resolve --mode stage --snapshot <snapshot-id> --path src/lib.rs --start 42 --json- Recover stale selections without mutating anything:
bash
git-hunk validate --mode stage --snapshot <snapshot-id> --change-key <change-key> --compact --json- 扫描仓库并捕获快照:
bash
git-hunk scan --mode stage --json- 检查可选单元:
bash
git-hunk show --mode stage <hunk-id>
git-hunk show --mode stage <change-id> --json
git-hunk show --mode stage <change-key> --json- 应用选择项:
bash
git-hunk stage --snapshot <snapshot-id> --hunk <hunk-id>
git-hunk stage --snapshot <snapshot-id> --change <change-id>
git-hunk stage --snapshot <snapshot-id> --change-key <change-key>
git-hunk stage --snapshot <snapshot-id> --hunk <hunk-id>:new:41-44
git-hunk stage --snapshot <snapshot-id> --change-key <change-key> --dry-run --json- 直接提交精确选择的内容:
bash
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change <change-id>
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change-key <change-key>
git-hunk commit -m "fix: message" --snapshot <snapshot-id> --hunk <hunk-id>:old:18-22
git-hunk commit -m "feat: message" --snapshot <snapshot-id> --change-key <change-key> --dry-run --json- 将文件+行提示解析为持久化选择器:
bash
git-hunk resolve --mode stage --snapshot <snapshot-id> --path src/lib.rs --start 42 --json- 恢复过时的选择项且不修改任何内容:
bash
git-hunk validate --mode stage --snapshot <snapshot-id> --change-key <change-key> --compact --jsonWorkflow
工作流
Stage mode
暂存模式
- Use for worktree changes relative to the index.
scan --mode stage - Select by whole hunk, ,
change_id, or line range.change_key - Prefer for agents; ids and
--jsoncome from scan output.snapshot_id - Use when you want short previews and metadata without the full line arrays.
scan --compact --json - Use selector bundles from JSON output instead of reconstructing selectors by hand.
- 使用查看工作区相对于索引的变更。
scan --mode stage - 可按完整代码块、、
change_id或行范围进行选择。change_key - 对于Agent,优先使用格式;ID和
--json来自扫描输出。snapshot_id - 当你需要简短预览和元数据而非完整行数组时,使用。
scan --compact --json - 优先使用JSON输出中的选择器包,而非手动重构选择器。
Unstage mode
取消暂存模式
- Use for staged changes relative to
scan --mode unstage.HEAD - Use the same selectors with to remove only part of the index.
unstage
bash
git-hunk unstage --snapshot <snapshot-id> --change <change-id>
git-hunk unstage --snapshot <snapshot-id> --change-key <change-key>
git-hunk unstage --snapshot <snapshot-id> --hunk <hunk-id>:old:10-12- 使用查看已暂存内容相对于
scan --mode unstage的变更。HEAD - 使用相同的选择器搭配命令,仅移除索引中的部分内容。
unstage
bash
git-hunk unstage --snapshot <snapshot-id> --change <change-id>
git-hunk unstage --snapshot <snapshot-id> --change-key <change-key>
git-hunk unstage --snapshot <snapshot-id> --hunk <hunk-id>:old:10-12Change keys
变更键(Change keys)
- is snapshot-bound and should be treated as ephemeral.
change_id - is currently
change_key_scheme.v1 - is derived from the change content plus nearby context so it survives unrelated rescans, duplicate disambiguation, and hunk splitting caused by unrelated edits.
change_key - is not guaranteed to survive nearby context edits, renames, or future scheme changes.
change_key - Prefer for multi-step agent workflows where a fresh
--change-keymay happen before mutation.scan - Use ,
show,stage, andunstagewith acommitexactly like achange_key.change_id
- 与快照绑定,应视为临时标识。
change_id - 当前版本为
change_key_scheme。v1 - 由变更内容及附近上下文派生而来,因此在无关重扫描、重复项消歧及无关编辑导致的代码块拆分场景中仍能保留。
change_key - 若附近上下文被编辑、文件重命名或未来方案变更,可能无法保留。
change_key - 在多步骤Agent工作流中,优先使用,尤其是在突变前可能会进行新扫描的场景。
--change-key - 使用、
show、stage和unstage命令时,commit的用法与change_key完全一致。change_id
Resolve helper
解析助手
- Use when you know a file and approximate line range but do not want to reason about diff internals.
resolve - returns recommended
resolves,change_ids, hunk selectors, selector bundles, and candidate metadata.change_key - is the default; it prefers
--side autolines innewmode andstagelines inoldmode.unstage
bash
git-hunk resolve --mode stage --snapshot <snapshot-id> --path src/lib.rs --start 42 --end 47 --json
git-hunk resolve --mode unstage --snapshot <snapshot-id> --path src/lib.rs --start 42 --side old --json- 当你知道文件和大致行范围,但不想处理差异内部逻辑时,使用命令。
resolve - 会返回推荐的
resolve、change_id、代码块选择器、选择器包及候选元数据。change_key - 默认使用;在暂存模式下优先选择
--side auto行,在取消暂存模式下优先选择new行。old
bash
git-hunk resolve --mode stage --snapshot <snapshot-id> --path src/lib.rs --start 42 --end 47 --json
git-hunk resolve --mode unstage --snapshot <snapshot-id> --path src/lib.rs --start 42 --side old --jsonValidation and dry-run previews
验证与预演(dry-run)
- Use to compare an old
validateagainst the current repo state and recoversnapshot_idselections before retrying a mutation.change_key - Use to preview what the index would look like after staging a selection.
stage --dry-run - Use to preview what would remain staged after removing a selection.
unstage --dry-run - Use to preview the exact files, diffstat, and patch that would be committed.
commit --dry-run - All dry-run commands use the real selection path against a temporary index, so they reflect actual behavior without mutating the repo.
- 使用命令将旧的
validate与当前仓库状态对比,在重试突变前恢复snapshot_id选择项。change_key - 使用预览暂存选择项后索引的状态。
stage --dry-run - 使用预览移除选择项后剩余的已暂存内容。
unstage --dry-run - 使用预览将被提交的精确文件、差异统计及补丁内容。
commit --dry-run - 所有预演命令都会在临时索引上执行真实的选择路径,因此能反映实际行为且不会修改仓库。
Line-range selectors
行范围选择器
- Syntax: .
<hunk-id>:<old|new>:<start-end> - Use when selecting added or replacement lines from stage mode.
new - Use when selecting the preimage side, especially in unstage mode.
old - Use without
showwhen you want numbered lines in terminal output.--json - Agents should almost always prefer , then
change_key, thenchange_id, and only use raw line ranges when reproducing a user-specified range exactly.resolve
- 语法:。
<hunk-id>:<old|new>:<start-end> - 在暂存模式下选择新增或替换行时,使用。
new - 在选择原始内容时使用,尤其是在取消暂存模式下。
old - 若要在终端输出中显示带编号的行,使用不带的
--json命令。show - Agent应始终优先使用,其次是
change_key,然后是change_id,仅在完全复现用户指定的范围时才使用原始行范围。resolve
Snapshot Discipline
快照规范
- Treat as mandatory for any mutating command.
snapshot_id - Rescan after every successful ,
stage, orunstage.commit - If the command returns , do not retry blindly; inspect
stale_snapshotor runerror.detailsto recover with the fresh snapshot.validate - can survive rescans, but the mutation still needs a fresh
change_keybefore it applies.snapshot_id
- 任何突变命令都必须指定。
snapshot_id - 每次成功执行、
stage或unstage后重新扫描。commit - 若命令返回,不要盲目重试;查看
stale_snapshot或运行error.details命令,结合新快照恢复操作。validate - 可在重扫描后保留,但突变仍需新的
change_key才能执行。snapshot_id
Plan Files
计划文件
Use a plan file when passing many selectors or when another tool is driving the workflow.
json
{
"snapshot_id": "s_123",
"selectors": [
{ "type": "hunk", "id": "h_abc" },
{ "type": "change", "id": "c_def" },
{ "type": "change_key", "key": "ck_xyz" },
{
"type": "line_range",
"hunk_id": "h_xyz",
"side": "new",
"start": 41,
"end": 44
}
]
}Run it with:
bash
git-hunk stage --plan plan.json --json
git-hunk stage --plan - --json < plan.json
git-hunk commit -m "refactor: split change" --plan plan.json --json当需要传递多个选择器,或由其他工具驱动工作流时,使用计划文件。
json
{
"snapshot_id": "s_123",
"selectors": [
{ "type": "hunk", "id": "h_abc" },
{ "type": "change", "id": "c_def" },
{ "type": "change_key", "key": "ck_xyz" },
{
"type": "line_range",
"hunk_id": "h_xyz",
"side": "new",
"start": 41,
"end": 44
}
]
}执行方式:
bash
git-hunk stage --plan plan.json --json
git-hunk stage --plan - --json < plan.json
git-hunk commit -m "refactor: split change" --plan plan.json --jsonFailure Handling
故障处理
- If you get , widen the range to cover the full atomic change or fall back to the
ambiguous_line_rangeshown bychange_id.scan - Use ,
error.category, anderror.retryablefrom JSON errors to decide whether to rescan, retry, or fall back.error.details - If a path appears under , do not try to force it through
unsupported; use normal git commands or a different workflow for conflicts, renames, copies, binary files, or non-UTF-8 diffs.git-hunk - If there is nothing staged, fails unless
commitis set.--allow-empty
- 若出现错误,扩大范围以覆盖完整的原子变更,或改用
ambiguous_line_range命令显示的scan。change_id - 利用JSON错误中的、
error.category和error.retryable决定是否重新扫描、重试或改用其他方案。error.details - 若路径出现在下,不要强行使用
unsupported处理;对于冲突、重命名、复制、二进制文件或非UTF-8差异,使用常规Git命令或其他工作流。git-hunk - 若没有已暂存内容,命令会失败,除非设置
commit参数。--allow-empty
Practical Defaults
实用默认规则
- Prefer over line ranges whenever both are available.
change_key - Prefer when you only have a file+line hint.
resolve - Prefer when an old snapshot goes stale but you still have
validates.change_key - Prefer ,
stage --dry-run, orunstage --dry-runbefore a risky atomic mutation from a dirty tree.commit --dry-run - Prefer with selectors when the user asked for a commit and you already know the exact changes.
commit - Prefer first when you need to inspect the staged result before committing.
stage - Keep commits atomic by scanning, selecting a minimal set, committing, then rescanning for the next commit.
- 只要可用,优先使用而非行范围。
change_key - 仅知道文件+行提示时,优先使用。
resolve - 当旧快照过时但仍有时,优先使用
change_key。validate - 在脏工作区执行高风险原子突变前,优先使用、
stage --dry-run或unstage --dry-run。commit --dry-run - 当用户要求提交且你已明确知道精确变更时,优先使用带选择器的命令。
commit - 若需要在提交前检查暂存结果,优先先执行命令。
stage - 通过扫描、选择最小变更集、提交、再扫描的方式保持提交的原子性。