24

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
Community
  • 1
  • 1
epsilones
  • 11,279
  • 21
  • 61
  • 85
  • 4
    You did WHAT?!?!? Why did you think that deleting packfiles would be a good idea? Do you have a backup of these packfiles or did you push your stuff anywhere? – Chronial Jan 28 '13 at 20:06
  • Yes I have them in some backups, what can I do with them ? – epsilones Jan 28 '13 at 20:23
  • @Chronial I have no problem with loosing all data in my current branch. I just want to get back another branch. Do you thin k it is possible ? – epsilones Jan 28 '13 at 20:28
  • Put your packfiles back in your repo, git needs them :). Or did you corrupt them by changing their content? Do you want to get back to branch that you already pushed? – Chronial Jan 28 '13 at 20:29
  • Indeed I have the pack files in my backup. I want to leave the branch I am currently on and I can loose it because I have it in some remote repo, and then go to another critical branch, that I don't have in my remote but only in my local. You think there's still some hope ? – epsilones Jan 28 '13 at 20:34
  • Yes – put the packfiles back where they belong, run `git fsck --full` and then try to check out your other branch. Update your question with the output of `git fsck` and the checkout. – Chronial Jan 28 '13 at 20:37
  • @Chronial, I update my post and it didn't work – epsilones Jan 28 '13 at 20:43
  • You put pack the files you corrupted by your search-and-replace, right? Have you tried to fix them by undoing your search-and-replace? – Chronial Jan 28 '13 at 20:47
  • sure I tried to undo the replace and fin but it didn't work... the situation is too bad... – epsilones Jan 28 '13 at 20:49
  • Looks like a bug. Consider creating new ticket to git development team – Sergei Krivonos Oct 22 '20 at 21:07

2 Answers2

40

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.

Gerald Hughes
  • 5,771
  • 20
  • 73
  • 131
Chronial
  • 66,706
  • 14
  • 93
  • 99
  • I began to run your command, and when putting this git unpack-objects -r < oldpack, I get a bunch of errors, is it normal: packfile .git/objects/pack/pack-3e7a0c040a5e3d1c21b91256d583424d82a59706.pack does not match index warning: packfile .git/objects/pack/pack-3e7a0c040a5e3d1c21b91256d583424d82a59706.pack cannot be accessed... – epsilones Jan 28 '13 at 21:05
  • Did the command run nevertheless or did it stop because of these errors? – Chronial Jan 28 '13 at 21:25
  • Yes it ran. I got the message I reported in the precedent message and then finishing by Unpacking objects: 100% (761/761), done. Should I continue unpacking the files with the same oldpack file ? – epsilones Jan 28 '13 at 21:30
  • 1
    Yes, unpack all the packfiles that are reported as corrupt. You can name them oldpack2 etc if you prefer. – Chronial Jan 28 '13 at 21:38
  • I updated my post, should I unpack the same error: packfile .git/objects/pack/pack-fbfd8042e1e96bf5ffff88f9b5a230b8f5e4d4c4.pack ? – epsilones Jan 28 '13 at 21:43
  • if you still get the error you did not mv the packfile away as per my first line, did you? – Chronial Jan 28 '13 at 21:44
  • So this file didn' appear at first, but I unpacked it. Now I have the messages telling me 'Your local changes to the following files would be overwritten by checkout'. Should I run git fsck --full again or commiting ? – epsilones Jan 28 '13 at 21:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23528/discussion-between-chronial-and-newben) – Chronial Jan 28 '13 at 21:50
  • 1
    (added some more info to the chat) – Chronial Jan 28 '13 at 22:15
  • 5
    Wrote the script to unpack all such "unaccessible" packfiles: https://gist.github.com/kirikaza/6178695#file-fix-git-packfiles-sh. – kirikaza Aug 07 '13 at 21:10
  • 7
    Had to run `git unpack-objects -r < oldpack` instead of `git-unpack-objects -r < oldpack` – Elijah Murray Jun 30 '15 at 18:58
  • You're the man! Thank you, it worked well. I almost resorted to recloning. Awesome! – Hani Sep 11 '19 at 02:22
-1

Rewrite this file with the same file from another .git copy of this repository.

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54