24

Immediately before receiving this error I did the following:

user@thismachine:~/file/path$ git add *
user@thismachine:~/file/path$ git push 
^C
user@thismachine:~/file/path$ git commit -m "my commitmesg"

(I panicked because I forgot to add a commit before pushing, so I cntrl+c'ed it.

Now, I receive the following error from git fsck --full:

error: inflate: data stream error (incorrect header check)
error: corrupt loose object '5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a'
fatal: loose object 5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a (stored in .git/objects/5c/deb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a)

git cat-file -t 5cdeb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a returns that this object is a commit.

After searching, I've found how fix this if the object is a blob but not if it's a commit.

GDorn
  • 8,511
  • 6
  • 38
  • 37
Will
  • 842
  • 2
  • 9
  • 16

6 Answers6

37

A solution from http://vincesalvino.blogspot.ca/2013/08/git-empty-files-corrupt-objects-and.html allowed me to fix the problem:

find .git/objects/ -size 0 -exec rm -f {} \;
sakovias
  • 1,356
  • 1
  • 17
  • 26
20

First, make a backup of your existing repository. cp -r or something. That way if your attempts to repair your repository screw it up worse you can restore.

Simplest thing to try is replacing that corrupt object file with a working one. If you have a backup of your repository, use that. Otherwise do a git clone from your remote repository to get a fresh copy and copy .git/objects/5c/deb9c3a1fe164cb4d2779d1e0d9d9f4ef18c6a into your existing broken one. See if that fixes it.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I have my repo on github but they're without these changes that I tried to commit (at this point, I don't care if I lose them forever, but would rather obtain them if possible). I cloned from my gh repo but it did not have any objects in its .git directory. :/ – Will Sep 24 '12 at 19:48
  • 7
    The objects are packed. Unpack them into individual files with `git unpack-objects .git/objects/pack/*.pack`. – Schwern Sep 25 '12 at 02:08
  • 2
    If git unpack-objects command does nothing "**nothing will be unpacked if** you use this command on a pack-file that exists within the target repository." ([git docs](http://git-scm.com/docs/git-unpack-objects)). To unpack the objects simply create a new repo using `git init`, and run the command to unpack the pack file (from the cloned repository) from the repository you just created. – Saulo Vallory Sep 26 '13 at 19:22
  • 4
    If you're doing @Schwern's solution and it seems like you haven't done the right arguments, (a) make sure you're at the top of the repository of course, and (b) try `git unpack-objects < .git/objects/pack/*.pack` (notice the redirection arrow). – floer32 Feb 11 '14 at 16:45
9

thanks for replying back. I ran that in the new cloned repo and returned that it unpacked 100% of the objects however they were not in .git/objects/pack of that repo.

So, instead and tried something this morning which worked. 1. cloning my github repository into a separate, new directory. 2. copying the locally changed files (that I wanted to commit originally) into my new cloned repository and then pushed them to github. 3. nuked my old local repository and 4. cloned it again to the same file path that I had my old repository.

Will
  • 842
  • 2
  • 9
  • 16
  • Although this response is an old one, it worked in my case where I had a corrupted object & couldn't compact the respository (via GitGUI). Before I copied over the local files I added a step - call it (1a) - compact the objects via GitGUI. This process was successful (finally!), after which I updated with the local objects I originally wanted to commit and committed successfully. Thanks! – NWdev Sep 05 '17 at 21:22
5

Very similar to Schwerm's answer, but I had to init a new repo into which to unpack the .pack files:

git clone <repo-uri> my_repo.fresh_clone
mkdir my_repo.newly_unpacked
cd !$
git init
for pack_file in ../my_repo.fresh_clone/.git/pack/*.pack; do
    git unpack-objects < $pack_file
done

Then I copied over the files from my_repo.newly_unpacked/.git/objects/<xx>/<sha1> as indicated by the error messages. I got caught out because a few operations, such as git checkout revealed more missing objects than a simple git status, so best to keep the restoration directories around for a bit before cleaning them up.

Paul Weaver
  • 284
  • 5
  • 9
0

Simple answer to this question for anyone facing this problem: the git clone command is the fix, if have a remote repo then clone it to the local folder (after deleting the corrupted local repo), in case you dont have remote repo then try to push the corrupt repo to github and then clone it from there, I think that corrupted objects wont be pushed and it will fix the problem

TooCooL
  • 20,356
  • 6
  • 30
  • 49
-2

I had a similar issue when the system crashed before doing a commit. Fortunately all I had to do was clone the full repo again to a new directory and discard the old one.

Newly cloned repo didn't have the .git/objects/#num/#hash file that was corrupt.

Vijay Kumar Kanta
  • 1,111
  • 1
  • 15
  • 25
  • This is not technically a solution as the OP looks like was trying to fix the issues on the repo without discarding it. – Sanjok Gurung Aug 13 '21 at 17:47