tags:

views:

218

answers:

1

I tried to open my repository today and it came up with no commit history. Everything I tried (git status, git log, git checkout...) threw an error about a corrupt object.

I researched this problem online and found the article by Linus Torvalds, but got lost at the point where he found the broken link ID: none of my file IDs, tree or blob, match the culprit ID thrown by the error message.

I then returned to the article on recovering "git objects damaged by hard disk failure" and (after moving the culprit object out of the way) worked my way through until $ cat packed-refs at which point my computer said: cat: packed-refs: No such file or directory I skipped that step and did the $ git fsck --full and got the appropriate output, but then I was supposed to copy the culprit (or what I was referring to as the culprit, the sha1 ID thrown by the error) from a backup repository back into the main repository, then copy the missing objects from the backup repository into the main repository, as far as I can tell; and I don't want to do anything too drastic or I might force something I can't unforce later.

So my question(s) is (are), was I supposed to have made a backup (ooh, newbie alert), or was that what happened when I unpacked the .pack file? And is the "culprit" I'm copying back actually a clean file, i.e. not corrupted?

(I think it only fair to tell you that I was initially confused by a simple dash in Torvalds' file between the "git" and "fsck." So I'm REALLY new at this.)

BUG-LIST:
Original bug:
$ git status
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

Bug after moving corrupt object:
$ git status
fatal: bad object HEAD
$ git fsck --full
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

$ git ls-tree 2c1033501b82e301d47dbf53ba0a199003af25a8
040000 tree 4a8b0b3747450085b1cd920c22ec82c18d9311bd folder1
040000 tree 33298295f646e8b378299191ce20b4594f5eb625 folder2
040000 tree dec82bad6283fc7fcc869c20fdea9f8588a2f1b2 folder3
040000 tree 4544967c6b04190f4c95b516ba8a86cab266a872 folder4

$ git ls-tree dec82bad6283fc7fcc869c20fdea9f8588a2f1b2
100644 blob 67bda6df733f6cd76fc0fc4c8a6132d8015591d8 fileA
100644 blob 4cb7272c9e268bfbd83a04e568d7edd87f78589c fileB
100644 blob ce9e0f2cc4d3b656fa30340afbdfed47fe35f3ef fileC

$ git ls-tree 4544967c6b04190f4c95b516ba8a86cab266a872
100644 blob d64fe3add8328d81b1f31c9dbd528956ab391fb6 fileD
100644 blob d1ebd7df7082abc5190d87caa821bf3edb7b68e8 fileE
100644 blob bb6cd264e47a3e5bc7beadf35ea13bac86024b02 ...
100644 blob 995d622b9012f4ef69921091d1e1a73f32aa94e6
100644 blob 9141dbd2b1c7931a6461195934b6599f5dfb485a
100644 blob ab128da1d82907cd0568448dc089a7996d5f79d3
100644 blob 57b11a7eb408a79739d2bb60a0dc35c591340d18
100644 blob 118105291c1c6ca4a01744889ffafbb018bc7ed3
100644 blob 86b1dfda56d0603f16910228327751f869d16bdc
100644 blob 077fe0cddde0d0be9d0974f928f66815caca7b76
100644 blob c0b32fd0450f21994bdc53ea83d3cf0bccd74004
100644 blob 37b87a4d11453468c4ae04572db5d322cd2d1d80
100644 blob 79d39f8d4e57fa3a71664598a63b6dfd88149638
100644 blob ee07bbe3e8cb5d6bb79fb0cd52cfbc9bd830498d files

$ git ls-tree 33298295f646e8b378299191ce20b4594f5eb625
100644 blob f9d6f45cd028aec97f761f00c5f4f2f6b50fb925 MoreFiles
100644 blob 0cb9eed1d0dd9214d54a03af1bda21f37b8c0d02
100644 blob 198e4f97ece735cce47b7e99b54f1b5fa99fabf5
100644 blob fc004212fa8e483e5a8ab35b508027c7a9a1cbfa
100644 blob 0c7d74c7a9a8337b4a9f20802b63d71d42287f89

$ git ls-tree 4a8b0b3747450085b1cd920c22ec82c18d9311bd
100644 blob 0320f5b23dd7cce677fac60b9ad03f418cff5c88 oneLASTfile

After moving the corrupted object back:
$ git log --raw --all
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

$ cat packed-refs
cat: packed-refs: No such file or directory

$ git fsck --full
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

After moving the file back out:
$ git fsck --full
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

After unpacking the .pack file:
$ git log
fatal: bad object HEAD

$ cat packed-refs
cat: packed-refs: No such file or directory

$ git fsck --full
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

+1  A: 

Okay, so. We can see from the second error message that the corrupt object which you moved was a commit. (HEAD was pointing to it!) Unfortunately, this means that it's hard to manually repair it. (By "hard" I mean likely impossible unless you can remember exactly what the commit message was and what time you made the commit.) Fortunately, this does mean that it's easy to resurrect a new commit with the same file contents - you'll just have to write a new message for it.

Before you start, have a look at the contents of .git/HEAD - if it's a branch name, remember that for later.

First, we need to figure out what the parent of this commit should've been. You can use git reflog to look at the reflog of HEAD, and find the SHA1 of where HEAD was just before you made commit 016660b. It should look something like this:

016660b HEAD@{n}: commit: <subject of commit>
1234abc HEAD@{n-1}: ...

You can copy the SHA1 of the previous position of HEAD, and check out that commit:

git checkout 1234abc

Then you can read in the tree that your corrupted commit had:

git read-tree 2c1033501b82e301d47dbf53ba0a199003af25a8

And then commit!

git commit

Now, there's some question here about what should've happened to your branches. If HEAD was pointing to a branch (say master) which in turn pointed to the corrupted commit, we definitely want to fix that up:

git branch -d master       # remove the original master branch
git checkout -b master     # recreate it here

If there are other branches which contained the corrupted commit, you'll have to do some restoration on them too - let me know if you need help with that.

Jefromi
With corrupt file displaced: $ git reflog fatal: bad object HEAD With corrupt file in place: $ git reflog fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted I read the manual and tried: $ git reflog show HEAD@{1} Which gave me activity from 1 to 12, and then restarted at 1and continued until the initial pull I had done on my repositories. Is this restart typical of the command?
Humble Penguin
Continuing on...$ git checkout bcbd203 #which was HEAD@{1}fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corruptedShould I simply move the file out and attempt the checkout again?Sorry about the absence of user-friendly formatting in these comments. I don't currently know a better way to do this.
Humble Penguin
@Humble: Oops, sorry about that. I haven't actually had this happen to me, so I was writing untested things. I didn't know reflog actually tried to read the objects! Anyway, as long as you've figured out what the previous commit was, that part's all good. (You could also have looked directly at `.git/logs/HEAD`)
Jefromi
@Humble: For the checkout... yeah, pretty much whatever you have to do to get it to succeed. Move the corrupt object out, and if that doesn't work... try `checkout -f`, then try `reset --hard <SHA1>`... once it works you should be able to continue on.
Jefromi
I did `checkout -f`, which gave me a polite `detached HEAD state` warning. It didn't hit me until after committing to the detached head that the `reset --hard <SHA1>` you mentioned might be to "undetach" (re-attach?) the HEAD. Was this the case or was it another level of advice in case `checkout -f` had failed? Two more small questions: if I delete the branch and then check it out again, do I lose the commit history? And do you have any ideas (besides backing up the repository) to prevent this from happening in the future?
Humble Penguin
The reset was in case the checkout failed, yes. You need to delete and recreate the branch there now. Lose the commit history? I believe all you've done is replace the broken commit with the new one you just made, so you will "lose" the broken one and gain the new one. As for prevention, in terms of software, backup is pretty much the way. (You could also look into RAID, but backups are still a good idea.)
Jefromi
SO CLOSE! It won't let me delete the branch because if the corrupt file is not there, it `Couldn't look up commit object for 'refs/heads/RPG'`, and if it is there, the `object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted`. I started to try forcing it (`git branch -d -f RPG`, `git branch -d RPG -f` and for kicks `git branch -D RPG`) but it threw the same errors or displayed the usage rules for `git branch`. Would it be best now to just force creation of the new branch on top of this old branch since it won't let me delete it? I would think not...
Humble Penguin
Aha! Git is just doing its best not to let you throw away any information - unfortunately we know we have to. You're going to have to get rid of the old branch one way or another. If `git branch -f master; git checkout master` doesn't work, just go in the .git directory and yank it out yourself - remove `refs/heads/master` and `logs/refs/heads/master`, then create it normally.
Jefromi
`git branch -f master; git checkout master` worked! I now have my repository up and working again. Thanks so much for your time and patience.
Humble Penguin
@Humble: Phew! Glad I managed to get you there eventually.
Jefromi