git commit -m with a path argument doesn't commit what you staged
git commit -m "msg" -- doesn't commit what you staged I'd staged a handful of files, run a narrow git commit -m "fix: ..." -- path/to/file.rs , and assumed the diff that landed matched what I'd git add ed. It didn't. It matched whatever was sitting on disk in that file at the moment the commit ran, which happened to include a few uncommitted lines from a different working session sharing the same…
When using the command `git commit -m "msg" -- path/to/file.rs`, it does not commit the files that you have staged. Instead, it commits only the current working-tree content of the specified file, disregarding the staged changes. This behavior can be dangerous because it commits whatever is on disk at the time of the commit, which may include uncommitted changes from a different session.
The index still affects other files in the repository, but for the paths named in the pathspec, git uses the current state of those files on disk. Running `git add --cached` beforehand does not resolve this issue, as the pathspec form of the commit command overrides the staged content. This problem is more noticeable in shared work environments, where multiple users may be editing the same files simultaneously.
To avoid this issue, it is recommended to stage exactly what you intend to commit before running the commit command. Running `git diff -- path/to/file.rs` after committing can help verify that the changes committed are indeed your own.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.