git error: object file $xyz is empty – Howto fix it! (tl/dr: use “git-repair”)

I just wanted to update my git repo with a pull and got the following error:

error: object file .git/objects/bf/75a18abe956a50b9ffbbbaed11d896a42dc278 is empty

I dont know why i got this error, but the fix is described at stackoverflow:

https://stackoverflow.com/questions/11706215/how-to-fix-git-error-object-file-is-empty

The solution wasnt in the first answer, or… it was, but more complicated than necessary, which is the reason i write this blogentry…

The solution was for me:

find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full

Which worked, but now i had a different error:

error: refs/heads/master: invalid sha1 pointer bf75a18abe956a50b9ffbbbaed11d896a42dc278

Nice!

So after an extended google search (about 20 seconds) i found the (real) solution:

git-repair

So i installed it with sudo apt install git-repair and let it run in the terminal, in my git-folder.

After waiting for an eternity (about 20 minutes on my notebook) git-repair did its magic and my problem was still there.

Well… it said i should run it with the “–force” option. I wish it would have suggested that before i waited for 20 minutes…

The message was:

Gogs: Repository owner does not exist                                                                                                                          
fatal: Could not read from remote repository.                                                                                                                  
                                                                                                                                                               
Please make sure you have the correct access rights                                                                                                            
and the repository exists.                                                                                                                                     
Trying to recover missing objects from remote origin.                                                                                                          
Gogs: Repository owner does not exist                                                                                                                          
fatal: Could not read from remote repository.                                                                                                                  
                                                                                                                                                               
Please make sure you have the correct access rights                                                                                                            
and the repository exists.                                                                                                                                     
5 missing objects could not be recovered!                                                                                                                      
To force a recovery to a usable state, retry with the --force parameter.

I made sure the repo still exists (im not the only contributor) and my user still exists, and that i have internet, and that the server is running, and that i can connect to the repo.

After that i typed git-repair --force and waited another eternity (thats defined to 20 minutes now, btw…)

After THAT… It worked… Kind of… The git is fixed now and i still have to figure out why i got the error in the first place and why git had problems connecting to the git-server…

So… if you have any problem with git: give git-repair a chance (and backup your git-folder before you do it – or dont… i didnt because im lazy and i could have cloned the repo from my git server anytime – but thats effort…)

TL/DR: sudo apt install git-repair && git-repair --force in your git-folder and be prepared to do something else in the eternity that it takes.

(Installing obviosly only if you havent installed it – but if you have installed it, you probably already used it, so you wouldnt read this…)

Leave a Reply

Your email address will not be published.