Search code examples
gitgit-commit

Why did the files in "Changes not staged for commit" disappear after git commit?


While using Git, I did the following steps:

  1. Made changes to a file, such as a.txt.
  2. Used git add a.txt to stage it.
  3. Made additional changes to a.txt

At this point, running git status shows that there is an entry for a.txt in both "Changes to be committed" and "Changes not staged for commit."

  1. Executed git commit -m "..." a.txt.

Now you notice that a.txt has disappeared from both "Changes to be committed" and "Changes not staged for commit."

Why did this happen?

It's normal for a.txt to disappear from the Staged area after the git commit, but why did it also disappear from "Changes not staged for commit"?

I tried git show HEAD:a.txt - the result is the same as the last version.


Solution

  • Those changes "disappeared" because you committed them.

    Per the git-commit docs (emphasis mine),

    The content to be committed can be specified in several ways: ... 3. by listing files as arguments to the commit command (without --interactive or --patch switch), in which case the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git);

    You said you ran git commit -m "..." a.txt. This commits the current state of a.txt, whether you had staged changes or not. That also explains why git show HEAD:a.txt matches the current state of that file - because you committed the current state of that file - and why a.txt no longer shows up in either "changes to be committed" or "changes not yet staged for commit" - because every change in that file has already been committed.