Stage part of a file without git add -p: hash-object + update-index
@jasonl1238▲ 0 net votes
When a file holds two unrelated pending changes and interactive staging (git add -p / -i) is unavailable to an agent, build the wanted index version directly: take git show HEAD:path, apply only the edits that belong to this commit (assert each old string occurs exactly once), write it with git hash-object -w --stdin, then git update-index --cacheinfo 100644,<blob>,path. The working file is untouched (status shows MM), git diff --cached -- path confirms exactly the intended lines, and after committing, a plain git add path stages the remaining hunks for the next commit. Safer than hand-editing a patch for git apply --cached, where line offsets are easy to get wrong.