Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repair a git repository with a missing object?

Tags:

git

My development repository at some point lost an object.

$ git fsck
fatal: failed to read object 2dddc84156fa30e4614a7ea5a1895885011b8db8: Invalid argument
$ git cat-file -t 2dddc84156fa30e4614a7ea5a1895885011b8db8
error: unable to find 2dddc84156fa30e4614a7ea5a1895885011b8db8
fatal: git cat-file 2dddc84156fa30e4614a7ea5a1895885011b8db8: bad file

A freshly cloned repository has that object.

$ git cat-file -t 2dddc84156fa30e4614a7ea5a1895885011b8db8
tree

I would like to keep my development repository, it has unpushed branches and stashes.

How can I patch that object into my broken repository?

like image 845
Schwern Avatar asked Dec 01 '11 20:12

Schwern


People also ask

What is git-repair?

git-repair can repair various forms of damage to git repositories. It is a complement to git fsck, which finds problems, but does not fix them. As well as avoiding the need to rm -rf a damaged repository and re-clone, using git-repair can help rescue commits you've made to the damaged repository and not yet pushed out.

How do you fix does not appear to be a git repository?

Note: The “fatal: 'origin' does not appear to be a git repository” error occurs when you try to push code to a remote Git repository without telling Git the exact location of the remote repository. To solve this error, use the git remote add command to add a remote to your project.


2 Answers

I recently had a bunch of missing objects in a local repository and an uncorrupted reference repository to restore it from. Here's the magic I came up with that worked for me:

cd <path-to-local-repo>
git unpack-objects < <reference-repo-path>/objects/pack/pack-<hash>.pack

It turns out that all of the objects I needed were in a pack file in the reference repository instead of in separate "loose" object files. This simple incantation unpacks that pack file into the local database, filling in the holes. After doing

git gc

to cleanup any dangling commits (i.e. due to rebasing, amend commits, etc.) my git fsck is now clean.

like image 147
campkeith Avatar answered Sep 20 '22 15:09

campkeith


What you can try, is to get the file .git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8 from the freshly cloned repository and copy it to your development repo ...

like image 43
klaustopher Avatar answered Sep 17 '22 15:09

klaustopher