git fsck错过报告树对象无法访问

时间:2016-05-03 06:18:34

标签: git

我希望git能够打印出所有无法访问的对象,而不考虑只能通过reflog条目访问的提交。 git fsck --unreachable --no-reflogs错过了打印我知道无法访问的树对象。我使用的是git 2.5.0版。我错过了什么吗?

以下是fsck无法打印的无法访问的树对象的证明。

$ git init
Initialized empty Git repository in /somepath/.git/
$ echo hello >myfile
$ git add myfile
$ git commit -m initial
[master (root-commit) 30a7654] initial
 1 file changed, 1 insertion(+)
 create mode 100644 myfile
$ echo world >>myfile

以下提交创建三个对象:提交,树和blob:

$ git commit -am 2nd
[master 4b96227] 2nd
 1 file changed, 1 insertion(+)

我们使这三个对象无法访问。 fsck仅将提交和blob对象报告为无法访问,但不报告树对象:

$ git reset --hard HEAD^
HEAD is now at 30a7654 initial
$ git fsck --unreachable --no-reflogs
Checking object directories: 100% (256/256), done.
unreachable commit 4b96227ebf01b6947dd5710b6494f5e92cd62d49
unreachable blob 94954abda49de8615a048f8d2e64b5de848e27a1

prune会将所有三个对象报告为无法访问,包括树对象:

$ git reflog expire --expire=all --all
$ git prune --expire now -vn
1df502bff6e2a54e686a0a5eb4033fb6cd160ba1 tree
4b96227ebf01b6947dd5710b6494f5e92cd62d49 commit
94954abda49de8615a048f8d2e64b5de848e27a1 blob

1 个答案:

答案 0 :(得分:2)

git fsck无法报告无法访问的树,这是正确的。您可以使用git write-tree创建其他无法访问的树,其中包含您未提交的索引(例如,git add某些内容,然后git rm --cached在写入树后使用相同的条目;或者使用{{1}在使用git update-index)之前和之后更直接地操纵索引。

认为 Git人认为这是一个特征而不是一个错误,理论上没有人关心树木。

请注意the documentationgit write-tree标志描述为恢复悬空提交和blob。它还(通过实验)恢复与其外部--lost-found引用脱钩的带注释标记对象,但是当它这样做时,它只是将对象名称写入refs/tags/中的文件名:

.git/lost-found/other/

扩展标记对象的内容会更有意义,就像Git对blob对象所做的那样。 Git甚至可以为树执行此操作,但正如$ cat .git/lost-found/other/9c6db16bcff193bd5a814ce5bcda40027713ed8a 9c6db16bcff193bd5a814ce5bcda40027713ed8a $ git cat-file -t 9c6db16bcff193bd5a814ce5bcda40027713ed8a tag 忽略它们一样,--unreachable也是如此。