Repair corrupted Git repository

前端 未结 9 1418
执念已碎
执念已碎 2020-11-30 06:27

My Git repository got corrupted after a couple of hard reboots due to power supply issues and now I\'m unable to fix it (I was in the middle of staging some files at the las

相关标签:
9条回答
  • 2020-11-30 06:54

    Try making a backup of the repository and then running git reset --hard HEAD@{1} to go back to the previous HEAD and see if this works. It may be just the current HEAD which is corrupted.

    (You should also run fsck on your disk if you haven't already.)

    0 讨论(0)
  • 2020-11-30 06:54

    Another alternative which worked for me was to reset the Git head and index to its previous state using:

    git reset --keep
    

    I also tried the following commands, but they did not work for me, but they might for you:

    git reset --mixed
    git fsck --full
    git gc --auto
    git prune --expire now
    git reflog --all
    
    0 讨论(0)
  • 2020-11-30 06:54

    I was able to recover my repository from:

    zsh(broken)% git log master
    error: object file .git/objects/7f/cab8648a989d9bb3f5246e6be7220395493395 is empty
    error: object file .git/objects/7f/cab8648a989d9bb3f5246e6be7220395493395 is empty
    fatal: loose object 7fcab8648a989d9bb3f5246e6be7220395493395 (stored in .git/objects/7f/cab8648a989d9bb3f5246e6be7220395493395) is corrupt
    zsh(broken)% cat .git/refs/heads/master
    7fcab8648a989d9bb3f5246e6be7220395493395
    e311726c4eb970f4d4f504ad86248d322855018f da9c14d03e4849394087b61ff6272399937f7cce Nikolay Orliuk <virkony@gmail.com> 1379583764 +0300    commit: plan: timings
    

    By resetting master to prev commit da9c14d03e4849394087b61ff6272399937f7cce as told by @Nash Bridges:

    zsh(broken)% echo da9c14d03e4849394087b61ff6272399937f7cce > .git/refs/heads/master
    zsh(broken)% git log --oneline -1 master
    da9c14d plan: timings
    zsh(broken)% git fsck
    Checking object directories: 100% (256/256), done.
    error: object file .git/objects/0e/ace931fdc851da254e9522596d1517d0ed51c5 is empty
    error: object file .git/objects/0e/ace931fdc851da254e9522596d1517d0ed51c5 is empty
    fatal: loose object 0eace931fdc851da254e9522596d1517d0ed51c5 (stored in .git/objects/0e/ace931fdc851da254e9522596d1517d0ed51c5) is corrupt
    

    Creating a new empty repository, fetching master from broken:

    zsh(broken)% mkdir ../recover && cd ../recover && git init
    Initialized empty Git repository in /home/nikolay/talks/y/recover/.git/
    zsh(recover)% git fetch ../broken master
    remote: Counting objects: 44, done.
    remote: Compressing objects: 100% (44/44), done.
    remote: Total 44 (delta 20), reused 0 (delta 0)
    Unpacking objects: 100% (44/44), done.
    From ../broken
     * branch            master     -> FETCH_HEAD
    zsh(recover)% git reset --hard FETCH_HEAD
    HEAD is now at da9c14d plan: timings
    zsh% git fsck
    Checking object directories: 100% (256/256), done.
    

    To restore those changes that was on the way to master:

    zsh(recover)% rm -rf * && cp -a ../broken/* ./
    zsh(recover)% git add -u && git commit -m 'prepare for publishing'
    
    0 讨论(0)
  • 2020-11-30 06:55

    My solution for a similar situation was to replace a hash of the damaged object in .git/refs/heads/my-working-branch with a hash of previous commit (which can be found in .git/logs/HEAD).

    0 讨论(0)
  • 2020-11-30 07:00

    I followed the instructions found in Recovering from a corrupt Git repository:

    $ cd /tmp/
    $ git clone good-host:/path/to/good-repo
    $ cd /home/user/broken-repo
    $ echo /tmp/good-repo/.git/objects/ > .git/objects/info/alternates
    $ git repack -a -d
    $ rm -rf /tmp/good-repo
    

    It worked for me.

    0 讨论(0)
  • 2020-11-30 07:01

    The most simple solution for me: You should git clone in a new folder, then replace the clean new_folder/.git to the old folder (the broken folder). It has worked well for me!

    git clone ...(remote) new_folder
    mv old_folder/.git  old_folder/.git_old
    cp -R new_folder/.git  old_folder/
    
    0 讨论(0)
提交回复
热议问题