修改文件后,git 中保留了多少个文件版本?

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

所以我正在阅读 Version Control with Git, 2nd Edition 并且无意中发现了这一段(第 51 页,第 "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.

让我困惑的是加粗的句子。 所以假设我做了相反的事情,我更改了一个文件,然后我 运行 git add 在它上面,现在文件被暂存了。好吧,在我看来,现在我又有了两个不同版本的文件:一个在对象存储中捕获,另一个在我的工作目录中并引用了索引。区别仅在于索引现在引用工作目录中的文件而不是 repo 中的文件。

我是否遗漏了作者想要强调的一些见解? "git" 中可以存在多少个不同版本的文件? "git" 也不清楚,但我想这意味着工作目录 + 对象存储 + 索引。

如果您编辑文件但不添加它,git 将有 2 个版本,因为它不在对象存储中管理。添加后,您在本地拥有的版本现在是来自 git object store

所传达的关键概念是您已经理解的概念:git add 这一步对于防止工作树和索引彼此不同是必要的。

"Two different versions" 显然不是指 只有 两个版本可以存在;它强调工作树中的内容不包含在索引中,git 的新手往往无法理解这个概念。