Git 如何创建唯一的提交哈希,主要是前几个字符? [英] How does Git create unique commit hashes, mainly the first few characters?
问题描述
我发现很难理解 Git 如何创建完全唯一的哈希,即使在前 4 个字符中也不允许相同.我只能使用前四个字符在 Git Bash 中调用提交.是在算法中明确决定第一个字符是超"唯一的并且永远不会与其他类似的散列冲突,还是算法以相同的方式生成散列的每个部分?
I find it hard to wrap my head around how Git creates fully unique hashes that aren't allowed to be the same even in the first 4 characters. I'm able to call commits in Git Bash using only the first four characters. Is it specifically decided in the algorithm that the first characters are "ultra"-unique and will not ever conflict with other similar hashes, or does the algorithm generate every part of the hash in the same way?
推荐答案
Git 使用以下信息生成 sha-1:
Git uses the following information to generate the sha-1:
- 提交的源代码树(分解为所有子树和斑点)
- 父提交 sha1
- 作者信息(带时间戳)
- 提交者信息(没错,它们是不同的!还有时间戳)
- 提交信息
(关于完整的解释;查看这里).
(on the complete explanation; look here).
Git 不保证前 4 个字符是唯一的.在 Pro Git Book 第 7 章 是这样写的:
Git does NOT guarantee that the first 4 characters will be unique. In chapter 7 of the Pro Git Book it is written:
Git 可以为您的 SHA-1 值找出一个简短而独特的缩写.如果您将 --abbrev-commit 传递给 git log 命令,输出将使用较短的值但保持它们的唯一性;它默认使用七个字符,但如果需要保留 SHA-1 可以使它们更长明确:
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 只使用只要有必要的缩写来保持唯一性.他们甚至注意到:
So Git just makes the abbreviation as long as necessary to remain unique. They even note that:
一般来说,八到十个字符就足够了在一个项目中.
Generally, eight to ten characters are more than enough to be unique within a project.
举个例子,Linux 内核,这是一个相当大的项目超过 45 万次提交和 360 万个对象,没有两个对象SHA-1 的重叠超过前 11 个字符.
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 的极大不可能.
So in fact they just depend on the great improbability of having the exact same (X first characters of a) sha.
这篇关于Git 如何创建唯一的提交哈希,主要是前几个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!