git - 是否有重复的 SHA 提交?

标签 git sha1 git-commit

<分区>

每次您进行提交时,git/hg 都会生成一个 SHA 以在存储库的历史记录中唯一标识该提交。

假设我想 merge 两个存储库(我们不知道是哪个存储库)。 这就提出了一个问题:如果有人想要对同一个 merge 的 repo 进行特定的提交,是否会有一个重复的 SHA 散列,这会使 git 在获得该评论时感到困惑?如果不是这样,git 会做什么?

最终我想问题还在于:全世界的每个存储库是否都有重复的哈希值?

最佳答案

Is there duplicated hashes taken every repository of the whole world?

可能是,但极不可能。让我引用 Git book关于这个,其中包含一个非常说明性的示例:

A lot of people become concerned at some point that they will, by random happenstance, have two objects in their repository that hash to the same SHA-1 value. What then?

If you do happen to commit an object that hashes to the same SHA-1 value as a previous object in your repository, Git will see the previous object already in your Git database and assume it was already written. If you try to check out that object again at some point, you’ll always get the data of the first object.

[...]

Here’s an example to give you an idea of what it would take to get a SHA-1 collision. If all 6.5 billion humans on Earth were programming, and every second, each one was producing code that was the equivalent of the entire Linux kernel history (3.6 million Git objects) and pushing it into one enormous Git repository, it would take roughly 2 years until that repository contained enough objects to have a 50% probability of a single SHA-1 object collision. A higher probability exists that every member of your programming team will be attacked and killed by wolves in unrelated incidents on the same night.

简而言之:是的,SHA1 冲突在理论上是可能的,但在天文数字上不太可能,以至于 Git 根本不考虑这种情况。

关于git - 是否有重复的 SHA 提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888175/

相关文章:

Git amend - 在子分支上看不到修改提交更改的更改

git - "No remote for the current branch"是什么意思?

git - 权限被拒绝(公钥),我仍然收到错误

git - 共享 Git 存储库中的 .editorconfig

git - 拥有 git clean 历史记录的最佳实践是什么?

android - 如何修复 “Cause: invalid type code: 85”错误

php - php 中的 sha1 未正确存储在 mysql 中

c# - 在.NET Core中使用SHA-1

git - 运行 git commit 时与编辑器相关的错误?

git - Git 中 merge 和提交的混淆