git提交散列是否等于存储库状态? [英] Does a git commit hash equal a repository state?

查看:164
本文介绍了git提交散列是否等于存储库状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个git提交都被归为一个签名其内容的散列。
它是否也标记提交来自何处,或仅仅是用于散列计算的提交数据本身?



不同的措词:是不可能的(除散列冲突外)伪造第二个存储库,其头部提交具有完全相同的散列和相同的内容,但树的其余部分有所不同? 解决方案<第二个问题的答案是肯定的(这是不可能的,等等)。



第一个问题没有我认为你可能会因为提交哈希实际上只是基于提交数据。导致第二个问题答案的关键是提交数据包含这些关键项目,您可以在实际提交中看到:

 $ git cat-file -p HEAD 
tree 22abd5c3fed5e2f49fb71e10b39d8c4929e51fc7
parent 4ebdeb68ba87282f87c39d790ba17fe1e021cc97
parent 9eabf5b536662000f79978c4d1b6e4eff5c8d785
[snip]

行给出树的哈希值(它仅依赖于树内容)和第二行,在这种情况下,作为 HEAD 是合并提交 - 给散列的父提交。鉴于当前提交的哈希取决于其树和父目录的哈希,如果要构建具有不同历史记录或不同树的不同回购库,那么这些哈希将具有不同的哈希以便提交也有不同的散列。

(这里通常使用的技术术语是 Merkle Tree 。)


Each git commit is attributed a hash which "signs" its content. Does it also sign where the commit came from or is it just the commit data itself which is used for the hash calculation?

Differently phrased: is it impossible (apart from hash collisions) to forge a second repository with its head commit having the exact same hash and same content but the rest of the tree differing?

解决方案

The answer to the second question is yes (it is impossible, etc).

The first question is not as well formed as I think you might want, because a commit hash is in fact just based on the commit data. The key that causes the second question's answer is that "the commit data" includes these key items, which you can see in an actual commit:

$ git cat-file -p HEAD
tree 22abd5c3fed5e2f49fb71e10b39d8c4929e51fc7
parent 4ebdeb68ba87282f87c39d790ba17fe1e021cc97
parent 9eabf5b536662000f79978c4d1b6e4eff5c8d785
[snip]

The tree line gives the hash of the tree (which depends only on the tree contents) and the parent lines—two, in this case, as HEAD is a merge commit—give the hashes of the parent commits. Given that the hash of the current commit depends on the hash(es) of its tree and parent(s), if you were to construct a different repo with a different history or different tree, those would have different hashes so that the commit would also have a different hash.

(The technical term usually used here is Merkle Tree.)

这篇关于git提交散列是否等于存储库状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆