Difference in SHA Hash between git hash-object & git hash-object -t

不想你离开。 提交于 2019-12-06 12:10:09
VonC

You changed the type of the object you wrote the hash with.
From git hash-object

-t <type>

    Specify the type (default: "blob").

You went from the default blob to commit.

And the object actually written start with the object type, which is part of what the sha1 has to compute.
See:

Git calculates the SHA1 for a file (or, in Git terms, a "blob"):

sha1("blob " + filesize + "\0" + data)

That changes the content of what is taken into account by the sha1.
With -t commit, you modify that prefix (it is no longer 'blob'), and since the content is different, the sha1 is also different.

You can do a:

python -c "import zlib,sys;print repr(zlib.decompress(sys.stdin.read()))" < .git/objects/02/b365d4af3ef6f74b0b1f18c41507c82b3ee571: 

The first word will be the type of the content

For further reference check How Git Works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!