如何诊断和修复 git 致命:无法读取树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20870895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:30:50  来源:igfitidea点击:

How to diagnose and fix git fatal: unable to read tree

git

提问by cmhughes

I'm using gitto manage files on a project, and keep running into this problem.

git用来管理项目上的文件,并不断遇到这个问题。

When I run git statusI get the message

当我跑步时,git status我收到消息

fatal: unable to read tree e2d920161d41631066945a3cbcd1b043de919570

As I understand it, I should check the output of git fsck, and I receive

据我了解,我应该检查 的输出git fsck,然后我收到

broken link from    tree e09a42f248afff64336fbbec2523df97c26451ac
              to    tree e2d920161d41631066945a3cbcd1b043de919570
broken link from    tree e09a42f248afff64336fbbec2523df97c26451ac
              to    tree 9b0dd389bd7f9e8d257395d57e0881b7957f9698
broken link from    tree e09a42f248afff64336fbbec2523df97c26451ac
              to    tree 9e288a4ad60d63f342dfd18237980674426aa725
broken link from    tree e09a42f248afff64336fbbec2523df97c26451ac
              to    tree 2a04647337089f554fab8c49cfd37149e5f4fc9f
broken link from    tree e09a42f248afff64336fbbec2523df97c26451ac
              to    tree ea16658b45ce961adc3c3f519b0e8d9672918ca8

together with a lot of missing blobmessages.

连同大量的missing blob消息。

Following various resources (e.g Git - Broken Links, Missing & Dangling Trees) I have simply re-cloned the project from githuband started again.

根据各种资源(例如Git - Broken Links、Missing & Dangling Trees),我简单地重新克隆了该项目github并重新开始。

Once I have re-cloned the project, all is good for a few commits, and then the problem arises again. Re-cloning every time doesn't seem to be optimal, and seems to go against the idea of using git, and I'd like to try and understand what is going on. How can I diagnose and fix this problem?

一旦我重新克隆了该项目,几commit秒钟就一切正常,然后问题再次出现。每次重新克隆似乎都不是最佳选择,似乎与使用 的想法背道而驰git,我想尝试了解发生了什么。如何诊断和解决此问题?

采纳答案by VonC

Following my older recommendation, it boils down to being able to find a repo which actually contains the missing elements (here full trees).
This is what is shown in "How to fix corrupted git repository?".

按照我较早的建议,归结为能够找到一个实际上包含缺失元素的存储库(这里是完整的树)。
这就是“如何修复损坏的 git 存储库?”中显示的内容。

But if a full clone doesn't solve the problem, then it may be:

但是如果一个完整的克隆没有解决问题,那么它可能是:



Update July 2016, with Git 2.10 soon to be release, you now have:

2016 年 7 月更新,随着 Git 2.10 即将发布,您现在拥有:

git fsck --name-objects

See "How to fix git error broken link from tree to tree?" for more.

有关更多信息,请参阅“如何修复从树到树的 git 错误断开的链接?”。

回答by Mark Leighton Fisher

I would start with a fresh clone, then run git fsckon the unchanged, untouched clone. I'm wondering (as above) if your initial clone is corrupt, but in a way allowing you to perform a few operations before orphaning the trees.

我会从一个新的克隆开始,然后git fsck在未更改的、未受影响的克隆上运行。我想知道(如上所述)您的初始克隆是否已损坏,但在某种程度上允许您在孤立树之前执行一些操作。

回答by Duncan Babbage

This may or may not be the problem originally raised here, but note that you will see a "fatal: unable to read tree" error from git if the user account the git command is being run under is no longer able to read all files checked out from your repository due to an ownership/permissions issue.

这可能是也可能不是这里最初提出的问题,但请注意,如果运行 git 命令的用户帐户不再能够读取所有检查过的文件,您将看到来自 git 的“致命:无法读取树”错误由于所有权/权限问题,从您的存储库中删除。

So in many case the first thing to check should be that you've got ownership and permissions set correctly on the files in your working tree, as that may well fix the problem. :)

因此,在许多情况下,首先要检查的应该是您在工作树中的文件上正确设置了所有权和权限,因为这很可能会解决问题。:)

回答by risingfish

Something that just worked for me was stashing my changes, doing a git pull, and then popping the changes back off the stash stack. My corruption might of been pretty shallow, so it probably won't work for everyone but it's worth a try.

对我有用的东西是隐藏我的更改,执行 git pull,然后将更改从存储堆栈中弹出。我的腐败可能很肤浅,所以它可能不会对每个人都有效,但值得一试。

Here's the output I got when I stashed:

这是我藏匿时得到的输出:

work@home ~/code/project $ git stash
fatal: unable to read tree 5fd5f4d0425b42e5b478773fa643dd6fd4918188
fatal: unable to read tree 5fd5f4d0425b42e5b478773fa643dd6fd4918188
Saved working directory and index state WIP on master: d93430c Generic commit msg.
HEAD is now at d93430c An older generic message.

After that I pulled and then popped and it cleaned itself up. Again, YMMV.

在那之后,我拉了然后弹出,它自己清理了。再次,YMMV。