git

A pathspec commit takes the whole file, not your hunks

A formatting pre-commit hook restages whole files, so a partially staged file goes in whole and somebody else's unfinished work rides along.

Dawit Worku · 28 November 2026 · 2 min

Two of us were editing the same file. I staged my hunks, committed with a pathspec to keep the commit narrow, and shipped somebody else's half-finished work along with mine.

What a pathspec commit actually does

git commit -m "..." -- path/to/file does not commit the staged version of that file. It commits the file's working tree content. The index is bypassed for every path you list. So a carefully staged subset of hunks is irrelevant the moment you add a pathspec.

$ git add -p src/site.css
$ git diff --cached --stat
 src/site.css | 18 ++++++++++++++
$ git commit -m "feat(header): add the wordmark" -- src/site.css
$ git show HEAD --stat
 src/site.css | 64 +++++++++++++++++++++++++++++++++++++++++++++++
A working tree file with staged header hunks and unstaged icon hunks, and the resulting commit holding both
What the index held, and what the commit took.

That alone is worth knowing. The second half is worse.

The formatting hook closes the gap you left

A pre-commit formatter that writes files and restages them, which is most of them, will restage the whole file. So even a plain git commit after git add -p can sweep in the rest of the file, because the hook touched it and put all of it back in the index.

The combination means a partially staged shared file is close to impossible to commit cleanly while someone else has it open.

What to do instead

Split a shared file only once its other editor has finished. If you genuinely cannot wait, commit a temporary copy that holds only your hunks, then restore. And after any commit that involved a partially staged file, read git show HEAD and look for hunks you did not write.

The check takes ten seconds. Finding it later, in someone else's review, takes considerably longer.

※