Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixing a corrupt loose object as a commit in git

Tags:

git

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.

like image 944
Will Avatar asked Sep 24 '12 19:09

Will


4 Answers

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 {} \;
like image 161
sakovias Avatar answered Nov 18 '22 01:11

sakovias


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.

like image 26
Schwern Avatar answered Nov 18 '22 00:11

Schwern


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.

like image 44
Will Avatar answered Nov 18 '22 02:11

Will


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.

like image 5
Paul Weaver Avatar answered Nov 18 '22 01:11

Paul Weaver