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:
- How could this have happened?
- How can I restore it the proper way?
I ask for a proper way, because all I can think of is:
- Since the corrupted branch is still checked out, make a copy of all files manually.
- Change the ref in the HEAD file to a working branch.
- Delete the corrupted branch.
- Checkout a new branch with the same or a different name.
- Add the files from your backup and commit.
But that sounds too hacky for my taste.
Any ideas?