caffein
caffein

Reputation: 333

Git index file not identical after resetting back - why?

I'm trying to understand how the git index file works.

I have a local repository with some files, one of them test.txt. I was on commit X and saved the index file of that commit to a temp directory.

Then I edited test.txt added and committed it which created commit Y. After that I ran git reset --hard X and saved the index file of commit X after having reset to it from commit Y.

Then I compared both index files (from commit X and from commit X after having reset to it from commit Y) and they were not identical. How can this be ? Is there a timestamp in the file ? as far as I know the index should have been reset to its previous contents (?)

Upvotes: 0

Views: 115

Answers (2)

jthill
jthill

Reputation: 60295

After that I ran git reset --hard X

That's what did it: index entries have the hash codes for the checked out content and also the resulting filesystem timestamps in the work tree. When you did git reset --hard it updated the work tree, so those files have identical content but more recent timestamps than the previous checkout of the same content.

Upvotes: 2

matt
matt

Reputation: 535231

What does it matter what the index file is like? All that's important is what version of what files it lists. You can find that out with ls-files.

Example:

$ git init
$ echo "howdy" > howdy.txt
$ git add .
$ git commit -m "root"
$ echo "bonjour" >> howdy.txt
$ echo "byebye" > byebye.txt
$ git add .
$ git commit -m "first"
$ git ls-files --stage
100644 3abe061d90cd975b4bef1fa702caec7c0f320b29 0   byebye.txt
100644 e1f6537eb149ccead2e53cbf2da40291c07d904a 0   howdy.txt

OK, so those are the "contents" of the index. Now let's make a change and a new commit:

$ echo "extra line" >> howdy.txt
$ git add .
$ git commit -m "second"
$ git ls-files --stage
100644 3abe061d90cd975b4bef1fa702caec7c0f320b29 0   byebye.txt
100644 1ad49f2d780a4b46f68dd9bb1571c65ffc1dc660 0   howdy.txt

And now let's reset:

$ git reset --hard HEAD^
$ git ls-files --stage
100644 3abe061d90cd975b4bef1fa702caec7c0f320b29 0   byebye.txt
100644 e1f6537eb149ccead2e53cbf2da40291c07d904a 0   howdy.txt

As you can see, the index is "identical" to what it was, in the only sense that matters.

Upvotes: 1

Related Questions