Repair corrupted Git repository

GitCorruption

Git Problem Overview


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 last power failure):

$ git status
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git fsck
fatal: failed to read object 24377c609184c192f3f3c1733bac7115c1080758: Invalid argument
$ git branch -a
(...works, lists branches...)
$ git checkout someotherbranch
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git log
fatal: failed to read object 3d18855708b0f127d40c13c679559d7679228b69: Invalid argument
$ git log someotherbranch
(...works, shows commits...)

So, as you can see, my current branch is pretty screwed up, and I don't seem to be able to fix it. What can I try to repair this?

Git Solutions


Solution 1 - Git

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).

Solution 2 - Git

This just happened to me. I reclone the repository in a new folder and move my latest changes over manually. Low tech, but it works every time. Hopefully you can remember your last changes.

Solution 3 - Git

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/

Solution 4 - Git

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.)

Solution 5 - Git

For me, I had enabled TRIM in OS X with a non-Apple SSD (which is not recommended) and apparently caused various corruptions on my boot disk. So the corrupted commit was deep in the history.

I don't care too much about repairing my repository, except I have a few local branches that were too experimental to bother pushing to the remote repository, and I'd like to salvage the work in those branches.

Theoretically, since this is a local repository, I feel that Git should be able to recover/repair itself using origin. Why isn't this possible?

At any rate I stumbled across this cool strategy to push a branch to another local Git repository. Unfortunately, cloning the repository into ../repo_copy and then using that as a local remote resulted in the following error:

! git push --force local_remote HEAD
fatal: failed to read object e0a9dffddeeca96dbaa275636f8e8f5d4866e0ed: Invalid argument
error: failed to push some refs to '/Users/steve/Dev/repo_copy'

So I started instead with an empty repository, and then pushing branches to it worked OK. So for any local branch I had whose git log didn't end in:

....
    Fixing cukes
fatal: failed to read object e0a9dffddeeca96dbaa275636f8e8f5d4866e0ed: Invalid argument

I simply would check it out and then do git push --force local_remote HEAD. The last thing I did was:

! cd ~/Dev/repo_copy
! git remote add origin [email protected]:sdhull/my_repo.git  # real remote

Then I went in to git config -e and set up my master branch and was back up and running with nothing lost!

Solution 6 - Git

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

Solution 7 - Git

I had the same problem and did the following steps using git-repair

  • cp myrepo myrepo.bak
  • cd myrepo
  • git repair --force (first try it without force)

After this was successful the tree was set back to the last working commit.

Then I did meld myrepo myrepo.bak to apply changes from the working tree of the corrupted repository to the fixed repository.

Solution 8 - Git

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 <[email protected]> 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'

Solution 9 - Git

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.

Solution 10 - Git

Windows gave bluescreen while I was changing branches. Booted back up and all files were corrupted and git did not recognize the repository.

How I fixed it:

  1. I cloned a fresh copy from remote.
  2. Copied over .git/index and .git/HEAD files over to corrupted .git repo.
  3. git finally recognized repo, so I did a hard checkout.
  4. Success

Note: If you had uncommitted changes this method will override them.

Solution 11 - Git

If it's not possible to repair, sometimes it's useful to retrieve the contents. The following command will print all the contents of uncorrupted objects.

$ git cat-file --batch-check --batch-all-objects 2>&1 | grep blob | grep -v empty | awk '{ print $1; }' -

Solution 12 - Git

I had some corrupted files and fixed them with git-repair:

sudo apt install git-repair 
git repair

Solution 13 - Git

git-repair (sudo apt install git-repair) and a few additional commands worked for me:

  1. Create a backup copy of your corrupted repository.

  2. Delete broken references:
    find .git/refs -size 0 -delete -print

  3. Repair repository from remote(s):
    git-repair --force

  4. Clean up dangling commits:
    git gc --prune=now

  5. Fetch the latest state from remote:
    git fetch

Optionally switch to master and reset it to origin/master:
git checkout master
git reset --hard origin/master

Verify with git fsck:
git fsck

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnknownView Question on Stackoverflow
Solution 1 - GitNash BridgesView Answer on Stackoverflow
Solution 2 - GitnbushnellView Answer on Stackoverflow
Solution 3 - GitSanjiMikaView Answer on Stackoverflow
Solution 4 - GitMichael MiorView Answer on Stackoverflow
Solution 5 - GitsteveView Answer on Stackoverflow
Solution 6 - GitgaborousView Answer on Stackoverflow
Solution 7 - GitstudentView Answer on Stackoverflow
Solution 8 - GitonyView Answer on Stackoverflow
Solution 9 - GitMrJView Answer on Stackoverflow
Solution 10 - GitSakoxView Answer on Stackoverflow
Solution 11 - GitTalespin_KitView Answer on Stackoverflow
Solution 12 - GitJesperView Answer on Stackoverflow
Solution 13 - GitPiotr Henryk DabrowskiView Answer on Stackoverflow