Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repair corrupted Git repository

Tags:

git

corruption

My Git repository got corrupted after a couple of hard reboots due to power supply issues and now I'm unable to fix it (I was in the middle of staging some files at the last power failure):

$ git status
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git fsck
fatal: failed to read object 24377c609184c192f3f3c1733bac7115c1080758: Invalid argument
$ git branch -a
(...works, lists branches...)
$ git checkout someotherbranch
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git log
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git log someotherbranch
(...works, shows commits...)

So, as you can see, my current branch is pretty screwed up, and I don't seem to be able to fix it. What can I try to repair this?

like image 379
Unknown Avatar asked Nov 25 '11 15:11

Unknown


People also ask

What is the effect if a file in the main repository becomes corrupted in git?

if your local . git folder gets corrupted, that corruption won't be propagated upstream to the server, so you should always be able to get a clean copy, minus your most recent changes. as for your question, most of the files in . git are not changed when you add a new commit.


3 Answers

My solution for a similar situation was to replace a hash of the damaged object in .git/refs/heads/my-working-branch with a hash of previous commit (which can be found in .git/logs/HEAD).

like image 174
Nash Bridges Avatar answered Oct 17 '22 06:10

Nash Bridges


This just happened to me. I reclone the repository in a new folder and move my latest changes over manually. Low tech, but it works every time. Hopefully you can remember your last changes.

like image 22
nbushnell Avatar answered Oct 17 '22 05:10

nbushnell


The most simple solution for me: You should git clone in a new folder, then replace the clean new_folder/.git to the old folder (the broken folder). It has worked well for me!

git clone ...(remote) new_folder
mv old_folder/.git  old_folder/.git_old
cp -R new_folder/.git  old_folder/
like image 15
SanjiMika Avatar answered Oct 17 '22 04:10

SanjiMika