1

I have a question about how git creates a hash for commit and tree objects. Hashing for blobs is totally clear to me (it just use SHA1 hash function to the content of some file), but how it works for commits and trees is not clear to me.

I created a local repository with one numbers.txt file and three commits.

git log gives the following:

commit dc41478273a498485995002d291651b80b81c7fa (HEAD -> master)
Author: somename <someemail@gmail.com>
Date:   Thu Jan 12 01:24:34 2023 +0100
 
   two

commit f367cb4266055586bc7bd03ce7ea439f647efe7c
Author: somename <someemail@gmail.com>
Date:   Thu Jan 12 01:24:26 2023 +0100

   one

commit a3ea6788d27f46adbcd2e3364863eb84fb0037f7
Author: somename <someemail@gmail.com>
Date:   Thu Jan 12 01:24:03 2023 +0100

   numbers.txt

Using the git command git cat-file -p f367cb4266055586bc7bd03ce7ea439f647efe7c (commit message one) I get the following:

tree 692ea16ed0b42594ae619008d7937e68774c6a74
parent a3ea6788d27f46adbcd2e3364863eb84fb0037f7
author somename <someemail@gmail.com> 1673483066 +0100
committer somename <someemail@gmail.com> 1673483066 +0100

one

Copying the above content in the SHA1 function results in 292a45fd8dab8ebeb1ae752c8e84d9168b500c6f (instead of f367cb4266055586bc7bd03ce7ea439f647efe7c).

The same applies when it comes to git tree objects. Using git cat-file -p 692ea16ed0b42594ae619008d7937e68774c6a74 (tree related to the commit used above) results in:

100644 blob 43dd47ea691c90a5fa7827892c70241913351963    numbers.txt

In this case, the SHA1 function gives ba31f041e553e4ff86e504af8b0a7a8169d5c23c (instead of 692ea16ed0b42594ae619008d7937e68774c6a74).

My questions:

  • How does git create a hash for commit objects AND tree objects?
  • Am I missing a space or a newline, or should I use some other content altogether?
Christian
  • 4,902
  • 4
  • 24
  • 42
Filip
  • 401
  • 8

0 Answers0