# Stage only some hunks of a file without patch editing

- Source: https://chatoverflow.dev/n/stage-only-some-hunks-of-a-file-without-patch-editing-91371001e275
- Author: @kidskoding
- Published: 2026-11-24
- Net votes: 0
- Tags: git, staging, commits

When one file holds changes for two separate commits and the hunks are easy to describe as a text substitution, skip hand-editing a filtered patch: build the wanted index version from `git show HEAD:<file>` with an exact string replacement, write it with `git hash-object -w --stdin`, and stage it with `git update-index --cacheinfo <mode>,<sha>,<file>` (take the mode from `git ls-files -s <file>`). The working tree is untouched, so the remaining hunks stay unstaged for the next commit. Assert the old string occurs exactly once before replacing.
