Moring Mark
Moring Mark

Reputation: 1

Why are the commit hash's different in the following toy repo?

It is the same file in all commits, just in different branches. And my understanding is that git does not use the branch name to calculate the hash.

Upvotes: 0

Views: 60

Answers (3)

Ajedi32
Ajedi32

Reputation: 48318

According to this gist, commit SHAs are calculated using all of the following information:

  • The source tree of the commit (which unravels to all the subtrees and blobs)
  • The parent commit sha1
  • The author info (name, email, and time)
  • The committer info (name, email, and time)
  • The commit message

In your case, it's likely that the commit time is different, which will result in a different commit id.

To view the exact information being used to calculate the commit hash, you can use the following command (replace HEAD with whatever commit you want to view the data for):

(printf "commit %s\0" $(git cat-file commit HEAD | wc -c); git cat-file commit HEAD)

You can verify this by piping the result of the previous command to sha1sum. You should get back the full ID of the commit you used in place of HEAD.

To find out exactly what's different between those two commits, you can run the following:

diff <(git cat-file commit 3fbf300) <(git cat-file commit 5bc715f)

If the trees have different ids, you can then run git diff 3fbf300 5bc715f to find exactly what the difference is between the files in each of those commits.

Upvotes: 0

Jonathan Turpie
Jonathan Turpie

Reputation: 1363

Try running git cat-file commit <branch/commit/etc>. That is what is run through SHA1 to produce the commit id (plus a header indicating the length of the data).

See https://gist.github.com/masak/2415865 for more information about how the commit ids are constructed.

Upvotes: 0

torek
torek

Reputation: 487725

There's not quite enough shown to be sure, but presumably it's because you made the commits at different times, or with slight changes to author and/or committer names, and/or with different commit messages.

The tree hashes should be the same, if all the files are identical. The commit hash is the SHA-1 of the commit contents, though, and the commit contents look like this:

tree 2d6f17582c301f3c19f64afbe70b629fcc68505b
parent 3dc5ce0a5686ede82b7ee20c895372f61a74f839
author Junio C Hamano <[email protected]> 1433188076 -0700
committer Junio C Hamano <[email protected]> 1433188076 -0700

Sixth batch for 2.5 cycle

Signed-off-by: Junio C Hamano <[email protected]>

(this is the current master branch for git, as of the time I ran git fetch). Note the author and committer time stamps.

Upvotes: 1

Related Questions