如何恢复我的 Git 存储库以解决“缺少树”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7236738/
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 11:43:52  来源:igfitidea点击:

How can I recover my Git repository for a "missing tree" error?

gitgerrit

提问by Justin Guertin

We are using Gerrit for our Git repository. On a project that has been active for several months, we are suddenly unable to push any changes. When we execute git pushwe see the following error:

我们将 Gerrit 用于我们的 Git 存储库。在一个活跃了几个月的项目上,我们突然无法推动任何变化。当我们执行时,git push我们看到以下错误:

error: unpack failed: error Missing tree 14d62f0ed4385e3f68f226ac133fa9932a9c65c9

error: unpack failed: error Missing tree 14d62f0ed4385e3f68f226ac133fa9932a9c65c9

Executing git fsck --fullyields:

执行git fsck --full产量:

dangling tree 2701c92fb6eab1826482f6bcc9598413e651a92a  
dangling blob c505b3b12cfacd1e26dec8dc559820a30fc20c27  
dangling blob 8907f94b5b4ec881e1b86d50681795e368c167ff  
dangling blob 4938d9fc34c6e2f92c68872bfec6e070b2b3a9c2  
dangling blob 8a5c8b8d35484ea1470e2d8e6c482fcb97b23d0f  
dangling commit 3072d3314e20bf6c6998e1c02986d83019d3e1df  
dangling tree 5377d7f7111d340854c3ee0946667c202227e603  
dangling tree c3783e9ab540457924ceb9f9fb5ea1c2b97472b1  
dangling blob 8b92d1765038e4ec5e721f98a2aabb305a7f9819  
dangling tree 38a03153f0f18ca15846e9bd4983a86800a43a94  
dangling commit 8db2d6ae5364174bebe13720a359ddb2e62d4c9d  

All of our developers are experiencing the same error when they try to push. So far, we've tried re-initializing the gerrit repo (git init --bare ...) and pushing up to it. We've also tried creating a new gerrit project with a separate repo. In the end, we keep getting the same error.

我们所有的开发人员在尝试推送时都遇到了同样的错误。到目前为止,我们已经尝试重新初始化 gerrit 存储库(git init --bare ...)并推动它。我们还尝试使用单独的 repo 创建一个新的 gerrit 项目。最后,我们不断收到同样的错误。

Does anyone have any insight on what the cause would be, or how to recover?

有没有人知道原因是什么,或者如何恢复?

回答by Filip

Use git push --no-thininstead of git push.

使用git push --no-thin代替git push

From Git docs:

来自 Git 文档:

A thin transfer significantly reduces the amount of sent data when the sender and receiver share many of the same objects in common. The default is --thin.

当发送方和接收方共享许多相同的对象时,精简传输会显着减少发送的数据量。默认值为--thin。

回答by erencan

I am getting this same error on my tortiuse git. I finally get the root cause of this error.

我在 Tortiuse git 上遇到了同样的错误。我终于得到了这个错误的根本原因。

The steps which causes this error;

导致此错误的步骤;

  • Create a new branch on head.
  • Do some modifications on new branchs
  • Somebody also make modifications on head branch
  • Try to push your branch
  • 在头上创建一个新分支。
  • 对新分支做一些修改
  • 有人也对头部分支进行了修改
  • 尝试推动你的分支

This error will occur if a local branch is created and not pushed until some modifications ara made in head branch. This is a normal thing, since remote head branch do not know anything about your local branch until a push action.

如果创建了本地分支并且直到在 head 分支中进行了一些修改后才推送,则会发生此错误。这是很正常的事情,因为远程头分支在推送操作之前不知道您本地分支的任何信息。

To solve this error, switch the head branch get a full pull action. Then switch your branch and try a push.

为了解决这个错误,切换头分支得到一个完整的拉动动作。然后切换您的分支并尝试推送。

回答by Jeff Ferland

Back it up... back it up right this second before you try anything.

备份它......在你尝试任何事情之前立即备份它。

Now, that sounds unfortunate. It's also a shame that it doesn't sound like you have a regular backup to go to. There is good news to be had, though: I bet your developers have this file, though it may be in a pack file. Try the following in somebody else's .git directory. Note that git uses the first two characters of the hash for the directory name.

现在,这听起来很不幸。听起来您没有定期备份也很遗憾。不过,有一个好消息:我敢打赌您的开发人员有这个文件,尽管它可能在一个包文件中。在其他人的 .git 目录中尝试以下操作。请注意,git 使用哈希的前两个字符作为目录名称。

find . -name d62f0ed4385e3f68f226ac133fa9932a9c65c9

If that shows up, copy that file to the same relative path on your server, and life should move on nicely. If not, then try this:

如果出现这种情况,请将该文件复制到您服务器上的相同相对路径,然后生活就会顺利进行。如果没有,那么试试这个:

find . -name \*.idx -exec cat {} \; | git show-index | grep 14d62f0ed4385e3f68f226ac133fa9932a9c65c9

That won't show you which pack file it is (you can quickly script this or do it manually), but it will tell you its there. Find the right pack file and expand it...

这不会向您显示它是哪个包文件(您可以快速编写脚本或手动执行此操作),但它会告诉您它在那里。找到正确的包文件并展开它...

git unpack-objects $FILE

From there, copy the file to the same relative path on your server. If that doesn't solve it, further work is needed. Swapping a developer's up-to-date-enough repository might fix things. You may also want to explore https://git.wiki.kernel.org/index.php/GitFaq#How_to_fix_a_broken_repository.3F, or post update comments and wait for me to get back around to this.

从那里,将文件复制到服务器上的相同相对路径。如果这不能解决它,则需要进一步的工作。交换开发人员的最新存储库可能会解决问题。您可能还想探索https://git.wiki.kernel.org/index.php/GitFaq#How_to_fix_a_broken_repository.3F,或发布更新评论并等待我回到这个问题。

回答by mtmattek

When we get this, I can almost always fix it with a git gc:

当我们得到这个时,我几乎总是可以用 git gc 修复它:

git gc --aggressive --prune=now

back up your git repo first!

首先备份你的git repo!

回答by Rose Perrone

Try a git pull --rebase.

尝试一个git pull --rebase.

I saved the diff (git show > ~/mychanges.txt, took out the commit msg at the top of the file). The checked out a new branch (git checkout -b newbranch) applied the changes (git apply ~/mychanges.txt), and then did a git pull --rebase. Then everything worked.

我保存了差异 ( git show > ~/mychanges.txt,取出文件顶部的提交信息)。签出的新分支 ( git checkout -b newbranch) 应用了更改 ( git apply ~/mychanges.txt),然后执行了git pull --rebase. 然后一切正常。

回答by Yassine Younes

I had the same problem. To solve this I used git fetch then I pushed again and it worked just fine.

我有同样的问题。为了解决这个问题,我使用了 git fetch 然后我再次推送,它工作得很好。

回答by Ahmad Baktash Hayeri

If not on master branch, you can simply delete the remote branch by:

如果不在 master 分支上,您可以通过以下方式简单地删除远程分支:

git push --delete origin <branch_name>

And then push your branch back to the remote:

然后将您的分支推回远程:

git push -u origin <branch_name>

回答by Shubham Vatsal

This generally happens when time of commit and push is different and which ultimately creates a mismatch between both the trees. Given a remote branch upstreamand local branch foo

这通常发生在提交和推送时间不同时,最终会导致两棵树之间不匹配。给定一个远程分支上游和本地分支foo

Firstly throw away all the uncommitted changes using

首先使用丢弃所有未提交的更改

git reset --hard foo

Then track the remote branch using

然后使用跟踪远程分支

git branch --set-upstream-to=upstream/foo

And finally

最后

git pull

回答by avgvstvs

Technically this answer's a little late, but what worked for me was to create a patch for the changeset I was trying to push, recloned the repository to another disk location, Applied the patch, recommitted, and pushed.

从技术上讲,这个答案有点晚了,但对我有用的是为我尝试推送的变更集创建补丁,将存储库重新克隆到另一个磁盘位置,应用补丁,重新提交并推送。

回答by Nagaraju

Quick solution is Fetch -> Rebase-> Commit and then Push.

快速解决方案是 Fetch -> Rebase-> Commit 然后 Push。