Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with corrupt git repo

Tags:

My git repo got corrupted while running a find and replace command (See here: Git reset failing after find and replace). So I deleted some pack files about which git told me that there were not reachable. So I ran rm ./.git/index, git reset. When running git commit, I got this message :

fatal: corrupt tree sha $someSHA

Finally git fsck, my working directory went clean and I was able to commit. The pb now is that when I run git checkout some_other_branch, I got this message

fatal: unable to read tree $someSHA

(The $someSHA appearing are the same). Someone could help me ?

Edit 1 :

when I run git fsck --full, I get a serie of

error: refs/remotes/repo0/master does not point to a valid object!
error: refs/remotes/repo1/new-version does not point to a valid object!
...

then a serie of

broken link from    tree d935b909f76ea92728d71038d0a67384353e65e1
              to    blob 05b97658ebd47fee25b76d80ac76cbd07d77961d
...

and then some missing blobs...

Edit 2 :

I ran git log --raw --all --full-history -- subdir/my-file, and I get

error: refs/remotes/repo0/master does not point to a valid object!
error: refs/remotes/repo1/new-version does not point to a valid object!
...
error: Could not read 9096eb9d9dcbdf15a04e0a7c78a3744936f82ac7
fatal: cannot simplify commit 8dfd8e3d5b698dc979300d93d8e89a757abf6ec6 (because of 9096eb9d9dcbdf15a04e0a7c78a3744936f82ac7)

Edit 3 :

I ran git fsck --full, the I get numerous lines as so

error: packfile .git/objects/pack/pack-fbfd8042e1e96bf5ffff88f9b5a230b8f5e4d4c4.pack does not match index
fatal: packfile .git/objects/pack/pack-fbfd8042e1e96bf5ffff88f9b5a230b8f5e4d4c4.pack cannot be accessed
...

when running git checkout to another branch I got numerous messages as so :

error: packfile .git/objects/pack/pack-3e7a0c040a5e3d1c21b91256d583424d82a59706.pack does not match index
warning: packfile .git/objects/pack/pack-3e7a0c040a5e3d1c21b91256d583424d82a59706.pack cannot be accessed
....
fatal: unable to read tree 2ad71d368b65eff0b6fec1ef72c6fdde6e80edad

Edit 4 :

following the suggested answer, I unpacked the files and when I checked out, I got some lines as so :

error: packfile .git/objects/pack/pack-fbfd8042e1e96bf5ffff88f9b5a230b8f5e4d4c4.pack does not match index
warning: packfile .git/objects/pack/pack-fbfd8042e1e96bf5ffff88f9b5a230b8f5e4d4c4.pack cannot be accesse....

and then a serie of lines beginning with

 error: Your local changes to the following files would be overwritten by checkout:
.idea_local_work/.name
.idea_local_work/libraries/sass_stdlib.xml
.idea_local_work/scopes/scope_settings.xml
... and then aborting
like image 954
epsilones Avatar asked Jan 28 '13 19:01

epsilones


People also ask

How can I fix a corrupted git repository?

git-repair can repair various forms of damage to git repositories. It is a complement to git fsck, which finds problems, but does not fix them. As well as avoiding the need to rm -rf a damaged repository and re-clone, using git-repair can help rescue commits you've made to the damaged repository and not yet pushed out.

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

If Repository becomes corrupted, then the WMI service will not be able to function correctly.

How do I fix a fatal Not a git repository?

Check that you correctly created the repo. If the directory doesn't contain a . git repo, use git init to properly initialize the repo or clone an existing repo. Make sure your HEAD file contains the correct information on your current branch.


1 Answers

First: Make a backup of your .git directory in case you corrupt things more in this process. Then:

  • Put back the best version of the packfiles you have available.
  • For each of the corrupt packfiles, run:

    mv .git/objects/pack/pack-**yourpack**.pack oldpack
    git unpack-objects -r < oldpack
    
  • Run git fsck --full and git checkout again, give us the output.

  • Looks like now you should be able to check out, but you will have to run git checkout -f **yourbranch**, as you have changes in your working dir that are not yet committed. These changes will get lost when you run git checkout -f, though.

like image 88
Chronial Avatar answered Sep 23 '22 21:09

Chronial