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

14

38

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 push we see the following error:

error: unpack failed: error Missing tree 14d62f0ed4385e3f68f226ac133fa9932a9c65c9

Executing git fsck --full yields:

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.

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

Mario answered 29/8, 2011 at 22:29 Comment(1)
H
52

Use git push --no-thin instead of git push.

From Git docs:

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.

Historian answered 23/9, 2015 at 14:54 Comment(1)
If your branch is ahead by many commits, you may want to use this option. It works fine!Magdalen
I
21

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

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 are 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.

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

Isotonic answered 8/7, 2015 at 6:58 Comment(1)
Wow that's a completely non-obvious cause. This solution worked for me!Chafee
L
13

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

Lincoln answered 28/6, 2018 at 15:31 Comment(0)
O
9

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.

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.

Olnee answered 29/8, 2011 at 22:52 Comment(2)
Thanks for the info, it helped me out quite a bit. I was able to find the right idx file, but unpacking the pack file didn't recover anything. It was the same story for some of our other devs, as well. In the end, I ran out of time, so we've dealt with the problem by initializing a new repo and keeping the old one around for history.Mario
We have a similar problem where we are unable to push certain branches due to a missing tree error. We have located the missing object and copied it to the remote repository but the problem persists. Any clues?Gearalt
C
7

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

git gc --aggressive --prune=now

back up your git repo first!

Curtin answered 29/6, 2017 at 20:46 Comment(0)
L
4

Try a 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.

Libretto answered 22/5, 2013 at 15:17 Comment(0)
P
3

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

git push --delete origin <branch_name>

And then push your branch back to the remote:

git push -u origin <branch_name>
Papa answered 1/3, 2015 at 5:3 Comment(0)
V
2

Try to do first

git gc

then do

git push

Got same problem. above solution worked for me.

Vadnee answered 24/9, 2020 at 6:58 Comment(0)
N
1

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 upstream and local branch 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
Neolith answered 8/12, 2017 at 13:35 Comment(0)
S
1

Weird, the --no-thin argument didn't work for me. What worked was a git pull (so probably git fetch.

Seacock answered 24/1, 2020 at 22:5 Comment(0)
C
0

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.

Carrel answered 10/7, 2014 at 13:1 Comment(0)
D
0

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

Decomposer answered 25/2, 2015 at 11:50 Comment(0)
P
0

I deleted my master branch and created once again. Its working . git branch -D master, git checkout master Luckily it working

Phelps answered 31/5, 2019 at 10:21 Comment(0)
N
0

In my case it turned out I forgot to git fetch before git rebase -i origin/master. Thus when I tried to push to gerrit, I got the above error.

Nolasco answered 17/7, 2019 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.