是否仅基于树的内容计算提交的SHA-1? [英] Is the SHA-1 of commits calculated based only on the content of the tree?
问题描述
为了进行实验,可以说您的 git log
标识了以下提交:
commit 16bc8486fb34cf9a6faf0f7df606ae72ad9ea438 //添加第二个文件
commit 9188f9a25b045f130b08888bc3f638099fa7f212 //初始提交
提交后, .git / refs / heads / master
指向16bc8486fb34cf9a6faf0f7df606ae72ad9ea438。
.git / refs / heads / master
文件指向9188f9a25b045f130b08888bc3f638099fa7f212 此时, git状态认识到一个新的未提交文件需要一些注意。
如果我确实提交了它,那么... git log
现在显示
提交b317f67686f9e6ab1eaabf47073b401d677205d5 //第二次提交的第二个文件
提交9188f9a25b045f130b08888bc3f638099fa7f212 //初始提交
问题1:
你会注意到,在我第一次提交第二个文件并且现在, SHA
哈希值是不同的。这是为什么?文件的内容没有改变,它仍然是一样的文件。
在这一点上,原来的第二次提交发生了什么?当我做 git show 16bc8486
时,它显示了这个提交。它不会出现在 git log
历史记录中。
问题1:由于生成的散列考虑了所有事情,包括提交元数据(本身包含日期和时间)。
问题2: git log
显示当前分支的日志。提交 16bc8486
不是它的一部分。据我所知(我不完全确定),垃圾收集器迟早会把它拿走,如果它发现它没有引用它(
For the sake of an experiment, lets say your git log
identifies the following commits
commit 16bc8486fb34cf9a6faf0f7df606ae72ad9ea438 // added 2nd file
commit 9188f9a25b045f130b08888bc3f638099fa7f212 // initial commit
After committing, .git/refs/heads/master
points to 16bc8486fb34cf9a6faf0f7df606ae72ad9ea438.
Let's say, after this, i manually edit the .git/refs/heads/master
file to point to 9188f9a25b045f130b08888bc3f638099fa7f212
At this point, git status recognizes that a new uncommitted file is in need of some attention. This is the same file my second commit took care of before.
If i do commit it .. git log
now shows
commit b317f67686f9e6ab1eaabf47073b401d677205d5 // 2nd file committed for the 2nd time
commit 9188f9a25b045f130b08888bc3f638099fa7f212 // initial commit
Question 1:
You'll notice that SHA
hashes are different between the very first time i committed a second file and now. Why is that? File's content did not change, it is still the same exact file.
Question 2
At this point, what happened to the original second commit? When i do git show 16bc8486
, it shows this commit. It does not however show up in git log
history.
Question 1: Because the hash is generated taking everything into account including the commit meta data (which itself contains the date and time).
Question 2: git log
shows the log of the current branch. The commit 16bc8486
is not part of it. As far as I know (I'm not completely sure) the garbage collector will take it away sooner or later, if it finds it with nothing referencing it (git gc --help
)..
这篇关于是否仅基于树的内容计算提交的SHA-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!