Search code examples
gitgit-index

How many versions of a file are kept in git when that file is modified?


So I am reading the Version Control with Git, 2nd Edition and I stumbled on this paragraph (page 51, section "Using git add")

Most of the day-to-day changes within your repository will likely be simple edits. After any edit and before you commit your changes, run git add to update the index with the absolute latest and greatest version of your file. If you don’t, you’ll have two different versions of the file: one captured in the object store and referenced from the index, and the other in your working directory.

What confuses me is the bolded sentence. So let's say I do the opposite, I change one file and then I run git add on it and now the file is staged. Well, it seems to me that now again I have two different version of the file: one captured in the object store and the other in my working directory and referenced the index. The difference is only that index now references the file in working directory rather than the one in repo.

Am I missing some insight here that author wanted to emphasize? How many different versions of the file can exist in "git"? The "git" is also not clear but I suppose it means on working directory + object store + index.


Solution

  • The critical concept being conveyed is one you already understand: That the git add step is necessary to prevent the working tree and the index from differing from each other.

    "Two different versions" is clearly not intended to mean only two versions can exist; it rather emphasizes that the content in the working tree is otherwise not included in the index, a concept which folks new to git often fail to grasp.