git-hunk
SKILL.md
Git Hunk
Use git-hunk to inspect, stage, unstage, and commit precise text changes without interactive prompts.
Prefer change_key over raw line ranges when you need selectors that survive unrelated rescans. Keep snapshot_id for safety, treat change_id as snapshot-local, and treat raw line ranges as a last resort.
Quick Start
- Scan the repo and capture a snapshot:
git-hunk scan --mode stage --json
- Inspect a selectable unit:
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:
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:
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:
git-hunk resolve --mode stage --snapshot <snapshot-id> --path src/lib.rs --start 42 --json
- Recover stale selections without mutating anything:
git-hunk validate --mode stage --snapshot <snapshot-id> --change-key <change-key> --compact --json
Workflow
Stage mode
- Use
scan --mode stagefor worktree changes relative to the index. - Select by whole hunk,
change_id,change_key, or line range. - Prefer
--jsonfor agents; ids andsnapshot_idcome from scan output. - Use
scan --compact --jsonwhen you want short previews and metadata without the full line arrays. - Use selector bundles from JSON output instead of reconstructing selectors by hand.
Unstage mode
- Use
scan --mode unstagefor staged changes relative toHEAD. - Use the same selectors with
unstageto remove only part of the index.
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
Change keys
change_idis snapshot-bound and should be treated as ephemeral.change_key_schemeis currentlyv1.change_keyis derived from the change content plus nearby context so it survives unrelated rescans, duplicate disambiguation, and hunk splitting caused by unrelated edits.change_keyis not guaranteed to survive nearby context edits, renames, or future scheme changes.- Prefer
--change-keyfor multi-step agent workflows where a freshscanmay happen before mutation. - Use
show,stage,unstage, andcommitwith achange_keyexactly like achange_id.
Resolve helper
- Use
resolvewhen you know a file and approximate line range but do not want to reason about diff internals. resolvereturns recommendedchange_ids,change_keys, hunk selectors, selector bundles, and candidate metadata.--side autois the default; it prefersnewlines instagemode andoldlines inunstagemode.
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
Validation and dry-run previews
- Use
validateto compare an oldsnapshot_idagainst the current repo state and recoverchange_keyselections before retrying a mutation. - Use
stage --dry-runto preview what the index would look like after staging a selection. - Use
unstage --dry-runto preview what would remain staged after removing a selection. - Use
commit --dry-runto preview the exact files, diffstat, and patch that would be committed. - All dry-run commands use the real selection path against a temporary index, so they reflect actual behavior without mutating the repo.
Line-range selectors
- Syntax:
<hunk-id>:<old|new>:<start-end>. - Use
newwhen selecting added or replacement lines from stage mode. - Use
oldwhen selecting the preimage side, especially in unstage mode. - Use
showwithout--jsonwhen you want numbered lines in terminal output. - Agents should almost always prefer
change_key, thenchange_id, thenresolve, and only use raw line ranges when reproducing a user-specified range exactly.
Snapshot Discipline
- Treat
snapshot_idas mandatory for any mutating command. - Rescan after every successful
stage,unstage, orcommit. - If the command returns
stale_snapshot, do not retry blindly; inspecterror.detailsor runvalidateto recover with the fresh snapshot. change_keycan survive rescans, but the mutation still needs a freshsnapshot_idbefore it applies.
Plan Files
Use a plan file when passing many selectors or when another tool is driving the workflow.
{
"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:
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
Failure Handling
- If you get
ambiguous_line_range, widen the range to cover the full atomic change or fall back to thechange_idshown byscan. - Use
error.category,error.retryable, anderror.detailsfrom JSON errors to decide whether to rescan, retry, or fall back. - If a path appears under
unsupported, do not try to force it throughgit-hunk; use normal git commands or a different workflow for conflicts, renames, copies, binary files, or non-UTF-8 diffs. - If there is nothing staged,
commitfails unless--allow-emptyis set.
Practical Defaults
- Prefer
change_keyover line ranges whenever both are available. - Prefer
resolvewhen you only have a file+line hint. - Prefer
validatewhen an old snapshot goes stale but you still havechange_keys. - Prefer
stage --dry-run,unstage --dry-run, orcommit --dry-runbefore a risky atomic mutation from a dirty tree. - Prefer
commitwith selectors when the user asked for a commit and you already know the exact changes. - Prefer
stagefirst when you need to inspect the staged result before committing. - Keep commits atomic by scanning, selecting a minimal set, committing, then rescanning for the next commit.
Weekly Installs
54
Repository
diodeinc/pcbGitHub Stars
198
First Seen
1 day ago
Security Audits
Installed on
claude-code54