gpt4 book ai didi

git - Git 如何创建唯一的提交哈希,主要是前几个字符?

转载 作者:IT王子 更新时间:2023-10-29 01:26:59 25 4
gpt4 key购买 nike

我发现很难理解 Git 如何创建完全唯一的哈希值,即使在前 4 个字符中也不允许相同。我可以仅使用前四个字符在 Git Bash 中调用提交。是在算法中明确决定第一个字符是“ super ”-唯一的并且永远不会与其他类似的哈希值冲突,还是算法以相同的方式生成哈希值的每个部分?

最佳答案

Git 使用以下信息生成 sha-1:

  • 提交的源代码树(展开所有子树和 Blob )
  • 父提交 sha1
  • 作者信息(带时间戳)
  • 提交者信息(对,它们是不同的!,还有时间戳)
  • 提交信息

(关于完整的解释;请查看 here)。

Git 保证前 4 个字符是唯一的。在 chapter 7 of the Pro Git Book它是这样写的:

Git can figure out a short, unique abbreviation for your SHA-1 values. If you pass --abbrev-commit to the git log command, the output will use shorter values but keep them unique; it defaults to using seven characters but makes them longer if necessary to keep the SHA-1 unambiguous:

因此,Git 只是根据需要 使缩写保持唯一性。他们甚至注意到:

Generally, eight to ten characters are more than enough to be unique within a project.

As an example, the Linux kernel, which is a pretty large project with over 450k commits and 3.6 million objects, has no two objects whose SHA-1s overlap more than the first 11 characters.

所以事实上,它们只是依赖于具有完全相同的(a 的 X 个第一个字符)sha 的极大不可能

关于git - Git 如何创建唯一的提交哈希,主要是前几个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com