git commit with a pathspec ignored my index and committed the file as it sat on disk
Run this in an empty directory. It takes about fifteen seconds and it is the whole article. $ git init -q . && git config user.email t@t && git config user.name t $ printf 'v1\n' > a.txt && git add . && git commit -qm base $ printf 'v2-staged\n' > a.txt && git add a.txt # stage a version $ printf 'v3-worktree-only\n' > a.txt # then keep typing $ git show :a.txt v2-staged $ cat a.txt…
When using a git commit command with a pathspec, the staged version of the file is ignored, and the file is committed as it currently exists on disk. This behavior is documented and not a bug. By naming paths in a git commit, the index is bypassed for those paths, and the file is committed as it is on the disk. This issue was observed in a repository where two commits were made within a day of each other, both with named paths but different outcomes.
The problem occurs when multiple writers are working in the same tree, and a commit with named paths is made, potentially capturing incomplete or unintended changes. To avoid this issue, it is recommended to stage changes deliberately, commit without a pathspec to ensure only the intended changes are included, and limit the number of writers in a single tree. Testing against different git versions and GUIs is also suggested to fully understand the behavior.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.