PeanutsMonkey
PeanutsMonkey

Reputation: 7095

Do parent tree hashes in git change when committing?

I performed the first commit and to see what the hash of the tree was I ran the command git cat-file -P HEAD which gave me the following output;

tree ac9b570150cca9243e1546f6c1b393f851dd7559
author pm 1358978176 +1300
committer pm 1358978176 +1300

I then proceed to add a new directory followed by a commit. I then ran the command git cat-file -p HEAD which have me the following output;

tree 297f145b042bf11f16ac39fa109df151a8d56ae3
parent dc2683fdf1bf9d5db5f1dc6fbb62576d10d57ae7
author pm 1358985313 +1300
committer pm 1358985313 +1300

I would thought that the parent hash was that of the preceding commit i.e. ac9b570150cca9243e1546f6c1b393f851dd7559. Have I not understood the use of parent hashes correctly?

Upvotes: 0

Views: 104

Answers (1)

larsks
larsks

Reputation: 311596

ac9b570150cca9243e1546f6c1b393f851dd7559 is, according to your information, the hash of the previous tree, not the previous commit. You don't show the initial commit id here, but git log will show it to you.

For example, given:

$ git log
commit be2ddc1cdc0cbe0dad791712806b5c155fa357fc
Author: Lars Kellogg-Stedman <[email protected]>
Date:   Wed Jan 23 19:13:27 2013 -0500

    second commit

commit 0e25b7e892bc6e637704909b3d66612807c8edc6
Author: Lars Kellogg-Stedman <[email protected]>
Date:   Wed Jan 23 19:13:03 2013 -0500

    initial commit

We can run git cat-file -p HEAD:

$ git cat-file -p HEAD
tree cdbd58dcba3cf89422f444a310979f6b40dde1ad
parent 0e25b7e892bc6e637704909b3d66612807c8edc6
author Lars Kellogg-Stedman <[email protected]> 1358986407 -0500
committer Lars Kellogg-Stedman <[email protected]> 1358986407 -0500

second commit

In this output, tree is the hash of the current tree, and parent matches the id of the previous commit.

Upvotes: 2

Related Questions