git - 如果两个 Git 提交具有相同的 SHA-1 哈希会发生什么?

标签 git

让我先声明一下,我知道发生这种情况的可能性极小。我知道制造或多或少是不可能的,而且不太可能“在野外”发生。这只是一个关于 Git 内部结构的假设问题。

那么,这是我的问题:如果两个 Git 提交哈希值相同会发生什么?对于初学者:

  • 提交会成功吗?
  • 以后是否可以将其作为分离的头进行检查?
  • 是否可以进行后续提交?

最佳答案

我的旧答案“How would git handle a SHA-1 collision on a blob?”仍然适用,即使是提交而不是 blob。
作为torek提及 in the comments , git 只是将所有事物都视为“对象”,每个对象都有自己的 SHA1。

https://git-scm.com/book/en/v2/book/10-git-internals/images/data-model-4.png

(图片来自 Git Internals - Git ReferencesProGit Book v2 章节)

虽然提交可能不会成功(git-commit-tree.c 中有一些检查),但您还必须考虑在 repo 协议(protocol) A 和 B 中创建具有相同 SHA1(并且内容不同)的两个提交的情况... repo A 正在获取 repo B!
Commit 8685da4 (March 2007, git 1.5.1)处理了这一点,提取就会失败。
Commit 0e8189e (Oct. 2008, git 1.6.1)确实提到了 index V2 :

the odds for a SHA1 reference to get corrupted so it actually matches the SHA1 of another object with the same size (the delta header stores the expected size of the base object to apply against) are virtually zero.

它在解包对象时仍然执行打包对象 CRC 检查。

Git code mentioned belowfinalize_object_file() function , 和 a blame没有显示最近的修改,大部分代码可以追溯到 Git(2005)的最开始:没有创建新的提交。

关于git - 如果两个 Git 提交具有相同的 SHA-1 哈希会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32619645/

相关文章:

git - 如何在 TortoiseGit 中从 Master 获取更新到 Branch

git - 是否可以通过 SSH 访问 Github Wiki?

windows - 如何编辑我的 .git/config 文件?

git - 在不透露过去提交的情况下公开 Git 仓库

git - 如何 checkout 我的远程/跟踪分支的 HEAD 版本

git - 为什么 Mac OS 的 SourceTree 不允许我以相同的方式分支两次?

git - 如何在 git 中使用分支跟踪引用?

Git 指纹与 Azure 不匹配

git - repo 同步错误 : . repo / list /:包含未提交的更改

java - 无法让 Github 在 intellij 上显示受控版本