Git repository corrupt (incorrect header check; loose object is corrupt)

后端 未结 3 1259
抹茶落季
抹茶落季 2020-12-04 20:45

I experienced a power failure yesterday evening while writing a commit message. When I booted the machine back up I couldn\'t complete the commit. I ran git reset

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 21:30

    It appears that git created files in .git/objects for the new commit, but didn't successfully write to them. I solved it by deleting them one at a time and re-running git fsck --full to find the next one. I started with the one originally reported by git fsck:

    % rm -f .git/objects/43/46883490a0990e68db0187241abc1642765a73
    % git fsck --full
    Checking object directories: 100% (256/256), done.
    error: inflate: data stream error (incorrect header check)
    error: unable to unpack 86e7247af5865e857a3b61eed99986e2d9538df1 header
    error: inflate: data stream error (incorrect header check)
    fatal: loose object 86e7247af5865e857a3b61eed99986e2d9538df1 (stored in .git/objects/86/e7247af5865e857a3b61eed99986e2d9538df1) is corrupt
    % rm -f .git/objects/86/e7247af5865e857a3b61eed99986e2d9538df1
    % git fsck --full
    Checking object directories: 100% (256/256), done.
    error: inflate: data stream error (incorrect header check)
    error: unable to unpack a94406345ac44982b00cf57b4b9660a35436637f header
    error: inflate: data stream error (incorrect header check)
    fatal: loose object a94406345ac44982b00cf57b4b9660a35436637f (stored in .git/objects/a9/4406345ac44982b00cf57b4b9660a35436637f) is corrupt
    

    And so on. I deleted five objects before git fsck came up clean, corresponding (as I suppose) to the five files in the commit I was trying to make. I guess that the file history was not corrupted at all.

    Incidentally, I thought of another method that seems to work as well. git clone copies the bad objects, but git push does not. After backing up, I created a new empty repository (--bare, because otherwise you can't push to master), then unstaged my changes and pushed both branches into the new repository. Then it was just a matter of checking it out again and restoring the latest changes from my backups.

    Still interested if anyone cares to shed light on the failure mechanism here.

提交回复
热议问题