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

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.

  • Jenkins Poll SCM (Git) on Windows master / Build on Linux slave
  • What is the difference between using -d and -D when deleting a branch in git?
  • git clone ssh permission denied
  • Git changelog: how to get all changes up to a specific tag?
  • How can I commit only deletes with Git?
  • Does Git generally choke on mounted drives? Or just Git GUI's?
  • $ 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?

  • Accurev externally, git internally
  • Automation of git pull using PHP code
  • Using 'git remote add origin' creates a global remote instead of one local to current directory/repo
  • Git files modified after checkout, reset --hard, etc. even though autocrlf is set to false
  • After svn2git, can you fetch changes from svn?
  • git-svn isn't committing to branches, only trunk
  • 3 Solutions collect form web for “How can I repair a git repository with a missing object?”

    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 …

    It pretty much boils down to this: do you have a backup of that file (.git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8) anywhere? If you can find it, copy it back to your git repository and everything will be good again. In other words, do you know if the repository has been cloned? If it has, you will find that file in the clone’s git folder and copy it to yours. If you have not cloned the repository, have you zipped it up somewhere? If you’re on a mac, have you checked Time machine? If you’re not on a mac, are you using something to backup your code?

    It basically boils down to this: You need a backup of that file. Git is designed so that when this happens, you just use the backup (which is a clone of the repository) to restore the file. That said, the file name is based on an SHA1 hash, so if you can find that filename in anyone else’s repository, chances are pretty good that you can use it in your own 🙂

    It is usually good practice to always clone your repositories to another location, for safe keeping in case something like this happens. This is also why services like github and bitbucket are so popular.

    You also probably want to look at this question: How to recover Git objects damaged by hard disk failure?

    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.

    Git Baby is a git and github fan, let's start git clone.