A weird thing happend to my git repository. When I try to commit something in tortoisegit window I receive all files from project. I cannot revert them, when I pull from server I receive fatal: No such ref: HEAD
and fatal: Cannot lock the ref 'HEAD'
. All my local branches are missing. Is there any way to resolve the problem?
This is not first commit or something. This thing happend suddenly.
EDIT:
git branch -a
says: Failed to resolve HEAD as a valid ref
git status
prints all project files marked as new file.
I changed repository folder name for a while, and when I changed it back things were not correct.
You've lost your HEAD
so you'll need to recreate it. The simplest thing to do is this.
echo ref: refs/heads/master >.git/HEAD
Now you should be able to run other git commands and see where you're at.
(Although, in theory, you could attempt to do git symbolic-ref HEAD refs/heads/master
newer git versions don't recognize a .git
as a git repository unless it already contains a HEAD
so this won't work to create a new one.)
HEAD
is usually a reference to a particular branch; in your case, it seems the branch pointers have gone missing, so the HEAD reference cannot be resolved.
You can use git fsck --lost-found
to scan the object cache for unreachable objects; specifically, you are interested in commits, which can then be found below .git/lost-found/commit/
; these are pointers to your branches, all you need to do then is find out which is which, and create new references using git branch
.
I think this answer maybe helpful for someone.
I resolved this problem nearly.
First what I did was, like Charles Bailey wrote, use
echo ref: refs/heads/master >.git/HEAD
Then my branch changed to master. I commited changes and was able to switch to my main branch. The problem was that I wasn`t able to use any of my local branches. Especially I wanted to work on branch 812. So I found last commit to branch 812 (create message when commit is very helpful ;)) and switched to it. Next I created branch 812 based on the one I switched to. Unfortunately some files were missing. Luckily I had them on the broken repo which I copied before 'echo'
For me the problem was that on Mac OS X either the 'uchg' or 'uappnd' flag was set, locking some git files regardless of the perms. I reset the chflags like this and it solved it for me:
sudo chflags -R 0000 .