Git 'fatal: No such ref: HEAD'

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 .