简体   繁体   English

git重用blob吗?

[英]Does git reuse blobs?

Let's say I committed a binary file, then changed it a couple of commits later and now I changed it back in a new commit. 假设我提交了一个二进制文件,然后稍后将其更改为几次提交,现在我在新提交中将其更改回来。

Out of curiosity, I wondered if git creates a new blob for it? 出于好奇,我想知道git是否为它创建了一个新的blob? Or does it detect it's in the history and reuse it? 或者它是否在历史中检测到并重复使用它? If so, how does it detect that? 如果是这样,它如何检测到? Checksum? 校验?

Git will reuse the same blob. Git将重用相同的blob。

I have done a test. 我做了一个测试。 I made 3 commits. 我做了3次提交。 First I commit a binary file, then I modified the binary file and commit it again. 首先我提交了一个二进制文件,然后我修改了二进制文件并再次提交。 Then finally I overwrote the file by original binary used in the first commit and commit again. 最后,我用第一次提交中使用的原始二进制文件覆盖了文件并再次提交。

The binary files content in 1st & 3rd commits are the same. 第1和第3次提交中的二进制文件内容是相同的。 Each commit is the HEAD of the follow branches: 每个提交都是以下分支的HEAD:

1st commit: "FIRST". 第一次提交:“第一次”。 2nd commit: "SECOND". 第二次提交:“第二次”。 3rd commit: "master" 第3次提交:“主人”

Then if you run "git cat-file -p FIRST^{tree}" , it shows hash code of the binary file. 然后,如果你运行“git cat-file -p FIRST ^ {tree}”,它会显示二进制文件的哈希码。

$ git cat-file -p FIRST^{tree}
100644 blob ec049240a47b472bd7c31d1fa27118c4fe2f1229    test.db3
$ git cat-file -p SECOND^{tree}
100644 blob a47bb3727e5aefe3ec386bec5520f3e4ffb3a4c5    test.db3
$ git cat-file -p master^{tree}
100644 blob ec049240a47b472bd7c31d1fa27118c4fe2f1229    test.db3

You will find that the hash code of the blob of 1st and 3rd commit are the same. 您会发现第1次和第3次提交的blob的哈希码是相同的。

git is so smart enough to check whether a blob exists for a hash code and reuse that blob if found. git非常聪明,可以检查是否存在哈希代码的blob,如果找到则重用该blob。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM