47

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.

tom
  • 21,844
  • 6
  • 43
  • 36
szaman
  • 6,666
  • 13
  • 53
  • 81

4 Answers4

68

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

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 1
    Ok, I made like you wrote. After echo I was on master branch, I made commit and was able to switch to my main branch. But now when I try to switch to any of my local branches I receive 'error: pathspec '812' did not match any file(s) known to git.' – szaman Jan 31 '11 at 08:56
  • 1
    @szaman: I don't know but it looks like the missing `HEAD` wasn't the only thing that's corrupt in your repository. Can you restore it from backup or re-clone? – CB Bailey Jan 31 '11 at 09:18
  • I resolved it... nearly. After I applied your tip I watched repo log. There was only last commit missing, so I created new branch based on last 812 which I found. Now I can switch to it. Files from last commit I can take from copy of broken repository, because I was working on 812 when this crash happend. Thanks a lot for help – szaman Jan 31 '11 at 09:23
  • ty. I somehow accidentally added "o\n" to HEAD through vim fugitive plugin. your answer helped to find that file. – Arnis Lapsa Jul 04 '12 at 13:49
  • 1
    WHere do I need to write this? – Boldijar Paul Nov 10 '14 at 07:29
  • @BoldijarPaul you can run it under your project directory with .git inside. – Yao Li Dec 06 '17 at 17:30
  • btw, replace `master` with your branch name, echo ref: refs/heads/your_branch_name >.git/HEAD – Yao Li Dec 07 '17 at 18:44
  • You've lost your HEAD so you'll need to recreate it. > love it. – Cokorda Raka May 23 '20 at 17:19
7

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.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64
  • Did you use the `--lost-found` option? – Simon Richter Jan 31 '11 at 08:18
  • Did it create the `lost-found` directory? – Simon Richter Jan 31 '11 at 08:49
  • Yes it created it but in commit folder is much less files than I had local branches – szaman Jan 31 '11 at 08:51
  • You will be missing all branches whose head is a parent commit for another branch. The `git fsck` command will find commits that are not pointed to by either another commit or a branch pointer, so if one branch is on top of another, then there is a commit object referencing the other branch tip, so no reference is created. – Simon Richter Jan 31 '11 at 09:02
6

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'

szaman
  • 6,666
  • 13
  • 53
  • 81
2

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 .