Non-interactive, programmatic git hunk staging with stable IDs.
Project description
git-hunk
Non-interactive, programmatic alternative to git add -p.
Every hunk gets a stable, content-based ID so you can inspect, filter, and stage changes without interactive prompts.
Why?
git add -p requires interactive input. That makes it unusable for:
- AI agents (Claude Code, Codex, etc.) that need to split changes into logical commits
- Scripts & CI/CD that automate commit organization
- Editor integrations that want hunk-level staging without shelling out to a TUI
git-hunk solves this by assigning each hunk a stable ID and exposing simple
stage/unstage/discard commands.
Install
pip install git-hunk
Or with uv:
uv tool install git-hunk
Verify it works:
git-hunk --version
Agent skill (optional)
For Claude Code, Codex, and other AI agents, add the skill via skills:
npx skills add wkentaro/git-hunk
Quick start
# See all hunks across staged, unstaged, and untracked files
git-hunk list
# Show the diff for a specific hunk
git-hunk show d161935
# Stage specific hunks, then commit
git-hunk stage d161935 a3f82c1
git commit -m "feat: add validation for user input"
# Stage the remaining hunks
git-hunk stage e7b4012
git commit -m "fix: handle empty response in API client"
Usage
List hunks
git-hunk list # all hunks (unstaged + staged + untracked)
git-hunk list --unstaged # unstaged hunks only
git-hunk list --staged # staged hunks only
git-hunk list src/foo.py src/bar.py # specific files
git-hunk list --json # JSON output for scripting
Show hunks
git-hunk show d161935 # show a single hunk
git-hunk show d161935 a3f82c1 # show multiple hunks
git-hunk show --all # show all hunks (staged + unstaged)
git-hunk show --all --staged # show all staged hunks
git-hunk show --all --unstaged # show all unstaged hunks
Stage, unstage, discard
git-hunk stage d161935 # stage a hunk
git-hunk stage d161935 a3f82c1 # stage multiple hunks
git-hunk stage d161935 -l 3,5-7 # stage specific lines only
git-hunk unstage d161935 # move back to working tree
git-hunk unstage d161935 -l 3,5-7 # unstage specific lines only
git-hunk discard d161935 # restore from HEAD
git-hunk discard d161935 -l ^3,^5-7 # discard excluding specific lines
JSON output
git-hunk list --json
[
{
"id": "d161935",
"file": "src/main.py",
"status": "unstaged",
"header": "@@ -10,3 +10,5 @@",
"additions": 2,
"deletions": 0,
"diff": "..."
}
]
Comparison
| Interactive | Programmatic | Hunk IDs | Line-level control | JSON output | |
|---|---|---|---|---|---|
git add -p |
Yes | No | No | Yes | No |
git add <file> |
No | Yes | No | No | No |
git-hunk |
No | Yes | Yes | Yes | Yes |
How it works
- Parses
git diffoutput into individual hunks - Assigns each hunk a stable, content-based ID (SHA-256 prefix)
- For staging: reconstructs a minimal patch and pipes it through
git apply --cached - For discarding: reconstructs a reverse patch and applies it to the working tree
IDs are stable across partial staging -- they are derived from the changed lines,
not the @@ line numbers that shift as you stage hunks.
Contributing
Bug reports, feature requests, and pull requests are welcome on GitHub.
git clone https://github.com/wkentaro/git-hunk.git
cd git-hunk
make setup # install dependencies
make test # run tests
make lint # run linters
License
MIT (LICENSE)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file git_hunk-0.1.0.tar.gz.
File metadata
- Download URL: git_hunk-0.1.0.tar.gz
- Upload date:
- Size: 320.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc4177cb733302b6c81e63f734a6e59ad62c628c9a94ef62c14bb11309f7d234
|
|
| MD5 |
faee25f8abbe0785ac146860472aa952
|
|
| BLAKE2b-256 |
17eea97e2e78a8b773e33e9dac0add688c076ffb56432645d7043a36b3c1585b
|
Provenance
The following attestation bundles were made for git_hunk-0.1.0.tar.gz:
Publisher:
publish.yml on wkentaro/git-hunk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
git_hunk-0.1.0.tar.gz -
Subject digest:
fc4177cb733302b6c81e63f734a6e59ad62c628c9a94ef62c14bb11309f7d234 - Sigstore transparency entry: 1234836060
- Sigstore integration time:
-
Permalink:
wkentaro/git-hunk@2def81424a72e2cb6fb495ca68fbe5daf3f83cb8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/wkentaro
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2def81424a72e2cb6fb495ca68fbe5daf3f83cb8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file git_hunk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: git_hunk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba09454db889cfa10f6bb9b639f558954a3873e3378cca65279d25cd3964599e
|
|
| MD5 |
07b783f525b441ba73e926dad204c482
|
|
| BLAKE2b-256 |
14335ffa5c109de70268c13b91407bdffef5964a1deb7b9e01951d5fdbee69c0
|
Provenance
The following attestation bundles were made for git_hunk-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on wkentaro/git-hunk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
git_hunk-0.1.0-py3-none-any.whl -
Subject digest:
ba09454db889cfa10f6bb9b639f558954a3873e3378cca65279d25cd3964599e - Sigstore transparency entry: 1234836063
- Sigstore integration time:
-
Permalink:
wkentaro/git-hunk@2def81424a72e2cb6fb495ca68fbe5daf3f83cb8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/wkentaro
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2def81424a72e2cb6fb495ca68fbe5daf3f83cb8 -
Trigger Event:
push
-
Statement type: