更新:我将两件事混合在一起:第一行实际上指向目录(在下面的答案中有很好的解释),第二行指向父提交。我让第二个误导了我。谢谢大家的澄清。
我在学git,正在抄代码she做。我在名为 Make a commit that is not the first commit 的小节中。在她 promise 之后,这就是她得到的(出于格式原因,我划掉了她的电子邮件):
tree ce72afb5ff229a39f6cce47b00d1b0ed60fe3556
parent 774b54a193d6cfdd081e581a007d2e11f784b9fe
author Mary Rose Cook ...
committer Mary Rose Cook ...
a2
而且,神秘地,我得到了(我划掉了我的个人信息)
tree ce72afb5ff229a39f6cce47b00d1b0ed60fe3556
parent c96fbf6143ccef645d1cb867b05427c399a9bcb3
author ....
committer ...
a2
比较当前树的两个哈希值,很明显我们得到了相同的哈希值(我确实完全按照她的代码)。
所以我很好奇这怎么可能?我知道 git 应该会散列那一刻的快照,但我的大数据肯定和她的不一样。有人知道会发生什么吗?
树哈希
是当前工作目录的哈希 - 换句话说,任何两个包含相同文件和目录的目录都将具有相同的树哈希。
A tree is a hierarchical collection of files and directories, not tied to any particular point in history. For example, if you create a file and then later delete the file (with no other intervening commits), you will end up with the same tree you started with.
A commit is a point in the history of your project. A commit specifies a tree, but also contains other information such as author/committer and time, a commit message (in which the author describes what changed), and most importantly zero or more parents, which are the previous state of the repository. (Your very first commit has zero parents. Most commits after that have one parent during linear development, and more than one if you merge.)
(Source)
一旦你做了一些改变并提交它们,你就会有一个commit hash
。 40 个字符的 SHA1 散列中发生散列冲突的可能性非常非常小。
An SHA-1 hash is a 40 hex character string... that's 4 bits per character times 40... 160 bits. Now we know 10 bits is approximately 1000 (10^24 to be exact) meaning that there are 1 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 different SHA-1 hashes... 10^48.
What is this equivalent of? Well the Moon is made up of about 10^47 atoms. So if we have 10 Moons... and you randomly pick one atom on one of these moons... and then go ahead and pick a random atom on them again... then the likelihood that you'll pick the same atom twice, is the likelihood that two git commits will have the same SHA-1 hash.
(Source)
您还可以从同一个父提交(parent hash
)创建两个或多个不同的提交,这也很好,但是每个提交哈希都会有所不同。除非你出人意料,否则请记住非常(不)幸运。