Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git repository broken after computer died

Tags:

git

My computer went dead and now one of my git repositories is broken. When I try to checkout master it tells me:

warning: ignoring broken ref refs/heads/master. error: Your local changes to the following files would be overwritten by checkout:         com.vainolo.jdraw2d.releng.p2/pom.xml Please, commit your changes or stash them before you can switch branches. Aborting 

When I execute git stash I get:

fatal: bad revision 'HEAD' fatal: bad revision 'HEAD' fatal: Needed a single revision You do not have the initial commit yet 

So... what can I do?

Update Output of git reflog:

fatal: bad default revision 'HEAD' 

Not very promising... Output of git fsck:

error: Invalid HEAD Checking object directories: 100% (256/256), done. error: unable to unpack 59551f96b4e87a1c14293c19eb548ce6fa1f196f header error: inflateEnd: stream consistency error (no message) fatal: loose object 59551f96b4e87a1c14293c19eb548ce6fa1f196f (stored in .git/objects/59/551f96b4e87a1c14293c19eb548ce6fa1f196f) is corrupt 
like image 481
vainolo Avatar asked Mar 09 '13 23:03

vainolo


2 Answers

I managed to recover through:

rm .git/refs/remotes/origin/HEAD git fetch --all 
like image 149
jfrumar Avatar answered Nov 16 '22 01:11

jfrumar


Start by following the steps suggested in Recovering broken git repository:

  • check whether .git/refs still contains anything useful
  • check git reflog and failing that the contents of .git/logs/refs/heads/master or whatever branch you were on last
  • run git fsck, potentially with --unreachable or --lost-found

This will hopefully allow you to figure out what the master ref should be so you can restore it (i.e. cat the correct SHA1 into .git/refs/heads/master).

In case any object contained in that commit is genuinely corrupted you can't restore your HEAD commit unfortunately. Assuming your working tree and/or index are intact you can try a git reset --soft (or failing that a git reset) to the previous commit and then re-do the commit. Avoid any operations that change your working tree s.a. git checkout -f or git reset --hard.

like image 41
kynan Avatar answered Nov 16 '22 01:11

kynan