The git
index allows us to specify the files that go into a new tree
object - such a tree
object is what any newly created commit
object will point to.
I'm aware that using git add <filename>
puts a file into the index so that a tree can be generated.
However, the index seems to store files even before they have been added to the index. After running the following command, even a "clean" directory will display all the same files as the commit pointed to by HEAD
.
git ls-files --stage
(I assume that this displays the files in the index
because stage
is synonymous with index
)
Why would git
store "un-added" files in the index? Aren't these files available by looking at HEAD
? This seems redundant.
The distinction between HEAD, index, and working tree gets even murkier when reading certain man
pages. For example, this is from man git-restore
.
By default, if --staged is given, the contents are restored from HEAD, otherwise from the index
- I would think that if
b.txt
is edited but not added to the index (i.e.git status
shows red letters), then the version ofb.txt
in theindex
is the same as inHEAD
. However,git restore --staged -- b.txt
does nothing whereasgit restore -- b.txt
restores the working tree to the version in theHEAD
commit. - If
stage
is synonymous withindex
then why would--staged
refer toHEAD
in thisman
page?