1

When I want to run git branch, git log or other commands in my repository I am facing following error:

fatal: Failed to resolve HEAD as a valid ref.

When I open .git/HEAD I see the branch I was expecting, because it was the last one I was working on:

ref: refs/heads/refactoring

When I open any file in .git/refs/heads/ I always find a single line with a string like that:

2d73344af3d39ab9c89df71f6696a1b0b65cdca9

But if I open .git/refs/heads/refactoring all I see are a bunch of zeros:

0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 00

So obviously Git cannot operate on that branch when there is no information in its HEAD file.

So two question:

  1. How could this have happened?
  2. How can I restore it the proper way?

I ask for a proper way, because all I can think of is:

  1. Since the corrupted branch is still checked out, make a copy of all files manually.
  2. Change the ref in the HEAD file to a working branch.
  3. Delete the corrupted branch.
  4. Checkout a new branch with the same or a different name.
  5. Add the files from your backup and commit.

But that sounds too hacky for my taste.

Any ideas?

Amberlamps
  • 39,180
  • 5
  • 43
  • 53
  • You don't have to copy files manually. Just create a new (temporary) branch. – musiKk Sep 26 '14 at 09:22
  • I cannot while HEAD is still pointing to the invalid branch – Amberlamps Sep 26 '14 at 09:44
  • Unfortunately I cannot reproduce the problem, I get different error messages if I try to. However, I don't think your solution is hacky if it works. You are in a corrupted repository so you may have to perform some unusual steps to get it repaired. If it works, you can provide that as an answer as well. It might help others that have the same problem. – musiKk Sep 26 '14 at 09:50
  • 1
    You can just manually change the HEAD file to point on a valid branch. Delete the corrupted branch file and create a new one. – Sascha Wolf Sep 26 '14 at 09:53
  • @Zeeker: I did exactly that. But if I want to commit the changes that once belonged to the corrupted branch I get: `error: inflate: data stream error (unknown compression method) error: unable to unpack 5ec6c4d5cdec15d206058ed6a475eb735f788ab8 header fatal: 5ec6c4d5cdec15d206058ed6a475eb735f788ab8 is not a valid object` – Amberlamps Sep 26 '14 at 09:55
  • Seems like your repository got corrupted somehow. Depending on the type of the object (commit, tree, blob) it can be easier or more difficult to restore a functional state without data loss. Maybe [this question](http://stackoverflow.com/questions/23725925/git-repository-corrupt-incorrect-header-check-loose-object-is-corrupt) can help you. – Sascha Wolf Sep 26 '14 at 10:03
  • @Zeeker: I posted exactly what I did step by step as an answer. Thanks for all the support. – Amberlamps Sep 26 '14 at 10:31

1 Answers1

2

I was able to restore my branch and here is how I did it:

  1. I did a backup of my files.
  2. Since the repo was still checkout out on the corrupted branch, I could not change between branches using git checkout validbranch. I had to set the branch by command line: echo ref: refs/heads/validbranch > .git/HEAD.
  3. Obviously all my changes that I did within that corrupted branch are now visible in the validbranch where they actually should not be.
  4. I created a new branch restorebranch and wanted to commit the changes to that branch. But that threw following error: error: inflate: data stream error (unknown compression method) error: unable to unpack 5ec6c4d5cdec15d206058ed6a475eb735f788ab8 header fatal: 5ec6c4d5cdec15d206058ed6a475eb735f788ab8 is not a valid object
  5. So I ran git fsck --lost-found which pointed my to the corrupted file: fatal: loose object abe0fd1236d488160187b91dbf4adeed31104355 (stored in .git/objects/ab/e0fd1236d488160187b91dbf4adeed31104355) is corrupt. I deleted that file manually. I had to run git fsck --lost-found several times because it only points you to corrupted files one at a time. I needed to delete approximately 10 corrupted files manually.
  6. Finally I was able to commit to restorebranch. I pasted my backup to my working directory. To my surprise there appeared no changes on my working branch. So I have no idea what kind of objects I actually deleted.
  7. When I wanted to delete the corrupted branch using git branch -D it told me error: branch 'refactoring' not found. even though it was clearly visible when I run git branch. To fix that I deleted .git/refs/heads/corruptbranch manually.

Everything is back to normal now, but there might be still some dead, unreachable objects in my .git directory that once belonged to corruptedbranch that are lost in limbo now.

Amberlamps
  • 39,180
  • 5
  • 43
  • 53
  • The limbo gets cleared approximatly every two weeks, so the objects should be able to rest in peace then. – Sascha Wolf Sep 26 '14 at 10:34
  • Btw. since you only touched the git object files it was to be expected that your working tree (the files you backed up) weren't changed. You probably lost a commit object which you deleted, but since you didn't make a hard reset or checked any files out your working tree was left untouched. I hope that was understandable, wanted to clear up your confusion. – Sascha Wolf Sep 26 '14 at 10:37
  • Alright, maybe I lost all commit objects that I made to the corrupted branch?! I deleted around 10 objects. It adds up with the number of commits I did to that branch. – Amberlamps Sep 26 '14 at 10:39
  • A commit object points to a tree object which points to blob objects and further tree objects. I assume some commit went wrong so quite some files got corrupted. Take a look at the [Git objects chapter](http://git-scm.com/book/en/Git-Internals-Git-Objects) of the gitpro book to understand how git manages the history internally. – Sascha Wolf Sep 26 '14 at 10:41