Git: "Corrupt loose object"
I had the same problem (don't know why).
This fix requires access to an uncorrupted remote copy of the repository, and will keep your locally working copy intact.
But it has some drawbacks:
- You will lose the record of any commits that were not pushed, and will have to recommit them.
- You will lose any stashes.
The fix
Execute these commands from the parent directory above your repo (replace 'foo' with the name of your project folder):
- Create a backup of the corrupt directory:
cp -R foo foo-backup
- Make a new clone of the remote repository to a new directory:
git clone [email protected]:foo foo-newclone
- Delete the corrupt .git subdirectory:
rm -rf foo/.git
- Move the newly cloned .git subdirectory into foo:
mv foo-newclone/.git foo
- Delete the rest of the temporary new clone:
rm -rf foo-newclone
On Windows you will need to use:
copy
instead ofcp -R
rmdir /S
instead ofrm -rf
move
instead ofmv
Now foo has its original .git
subdirectory back, but all the local changes are still there. git status
, commit
, pull
, push
, etc. work again as they should.
Your best bet is probably to simply re-clone from the remote repository (i.e., GitHub or other). Unfortunately you will lose any unpushed commits and stashed changes, however your working copy should remain intact.
First make a backup copy of your local files. Then do this from the root of your working tree:
rm -fr .git
git init
git remote add origin [your-git-remote-url]
git fetch
git reset --mixed origin/master
git branch --set-upstream-to=origin/master master
Then commit any changed files as necessary.
Looks like you have a corrupt tree object. You will need to get that object from someone else. Hopefully they will have an uncorrupted version.
You could actually reconstruct it if you can't find a valid version from someone else by guessing at what files should be there. You may want to see if the dates & times of the objects match up to it. Those could be the related blobs. You could infer the structure of the tree object from those objects.
Take a look at Scott Chacon's Git Screencasts regarding git internals. This will show you how git works under the hood and how to go about doing this detective work if you are really stuck and can't get that object from someone else.
Working on a VM, in my notebook, battery died, got this error;
error: object file .git/objects/ce/theRef is empty error: object file .git/objects/ce/theRef is empty fatal: loose object theRef (stored in .git/objects/ce/theRef) is corrupt
I managed to get the repo working again with only 2 commands and without losing my work (modified files/uncommitted changes)
find .git/objects/ -size 0 -exec rm -f {} \;
git fetch origin
After that I ran a git status
, the repo was fine and there were my changes (waiting to be committed, do it now..).
git version 1.9.1
Remember to backup all changes you remember, just in case this solution doesn't works and a more radical approach is needed.