Git 找不到悬空树的来源

Git can't find dangling tree origin

我是 运行 当前文件和外部驱动器上文件备份之间的差异检查器,我发现 git 存储库与其备份之间存在一些差异。唯一的区别是左侧的 .git/objects 下有一些额外的文件,右侧有两个空白的 .git/objects/info 和 .git/objects/pack 文件夹。我在两侧做了一个快速的 git showgit log,这给出了相同的输出。

然后我做了 git fsck --no-reflogs,在左侧发现了一个额外的悬挂提交。迭代地 git ls-tree 沿着我的方式进行额外的提交给了我一堆树和斑点,除了左侧的一个额外文件外,它们都占了。

在最后一个文件上使用 git cat-file -t <sha1> 告诉我它是一棵树。但是,我在每次提交时都使用了 git ls-tree,但仍然没有找到对这个神秘树对象的引用。那么这个文件到底是从哪里来的呢?我 99.99% 确定没关系,只是好奇 :P

嗯,菜鸟的错误。事实证明,额外的悬挂提交直接指向丢失的树。我认为执行 git ls-tree <sha1> on a commit object would give the tree it was pointing to, but it actually gives the children of the tree it's pointing to. Using git cat-file -p <sha1> 查找对象内容可以为我提供所需的信息。

所以似乎 .git/objects 中的所有文件都是提交或悬空提交。我发现 this 是关于 git 对象如何工作的非常简洁的指南。