Git如何创建独特的提交哈希,主要是前几个字符? [英] How does Git create unique commit hashes, mainly the first few characters?

查看:176
本文介绍了Git如何创建独特的提交哈希,主要是前几个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难围绕Git如何创建完全独特的哈希,即使在前4个字符中也不允许这些哈希是相同的。我只能使用前四个字符来调用Git Bash中的提交。在算法中是否特别决定第一个字符是超独特的,并且永远不会与其他类似的哈希冲突,或者算法是否以相同的方式生成哈希的每个部分?


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




>提交的源代码树(解开所有子树和
blob)

  • 父提交sha1

  • 作者信息

  • 提交者信息(正确,不同!)

  • 提交信息



  • (关于完整的解释,请查看此处)。



    Git 保证前4个字符是唯一的。在 Pro Git Book第7章中,它是书面:


    Git可以计算出SHA-1值的简短缩写。
    如果您将--abbrev-commit传递给git log命令,输出
    将使用较短的值,但保持唯一;它默认使用七个
    字符,但为了保持SHA-1
    毫不含糊,需要延长它们长度:

    因此,只要必要,Git只会缩写以保持唯一性。他们甚至指出:


    通常,在项目中,8到10个字符绰绰有余是唯一的
    。例如,Linux内核是一个非常大的项目,其中
    超过450k的提交和360万个对象,没有两个对象的
    SHA -1s重叠超过前11个字符。


    所以实际上他们只是依靠伟大的不可能性具有完全相同的(X的第一个字符)sha。


    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 uses the following information to generate the sha-1:

    • The source tree of the commit (which unravels to all the subtrees and blobs)
    • The parent commit sha1
    • The author info
    • The committer info (right, those are different!)
    • The commit message

    (on the complete explanation; look here).

    Git does NOT guarantee that the first 4 characters will be unique. In chapter 7 of the Pro Git Book it is written:

    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:

    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.

    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.

    So in fact they just depend on the great improbability of having the exact same (X first characters of a) sha.

    这篇关于Git如何创建独特的提交哈希,主要是前几个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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