Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fatal: Failed to resolve HEAD as a valid ref

I am getting fatal: Failed to resolve HEAD as a valid ref. whenever I try to commit.

I have tried

echo ref: refs/heads/master >.git/HEAD

but its not working

Also tried

git commit

its not working either from below sources

Git 'fatal: No such ref: HEAD' git tag: fatal: Failed to resolve 'HEAD' as a valid ref

Please help in..All my commit history is also gone

like image 311
idurvesh Avatar asked Jun 02 '16 14:06

idurvesh


4 Answers

I have also encountered the same issue, and have resolved it as follows:

  1. Clone your same project in some other folder.
  2. Copy the (hidden) .git folder of the cloned project.
  3. Finally replace the .git folder of the original project with the one that you've have copied.

Edit

Why does this happen?

  • Broken git repositories are often the result of file system corruption due to abrupt power failure or other abnormalities.
  • Since git stores all the information inside .git folder and if they are corrupted git can no longer recognize the repository.

Caveats

Everything in your previous .git folder will be gone. Configurations like remote reference name(s) must be set up again.

like image 68
Pravesh Khatri Avatar answered Nov 12 '22 23:11

Pravesh Khatri


I got this issue due a disk failure. I know this is an old question but maybe it can help someone. In my case I had a local branch where I was working on before my repo got corrupted so re-cloning the repo was not suitable for me. Any answer here or in other post helped me, except this little piece of code I found here. I just ran this command in the root of my repo:

echo ref: refs/heads/master >.git/HEAD

After that, I was able to excecute, git branch, git commit and all other git commands.

I hope this can help someone!

like image 23
Yasiel Cabrera Avatar answered Nov 12 '22 23:11

Yasiel Cabrera


My problem was with

git init
git add .

Tried

git reset
  fatal: Failed to resolve 'HEAD' as a valid ref.
git reset --hard
  fatal: Failed to resolve 'HEAD' as a valid ref.

Solved with

git rm -r --cached .

Enviroment

git version 1.7.5.4

Ubuntu 11.10

like image 5
hrvoj3e Avatar answered Nov 12 '22 23:11

hrvoj3e


In my case, I ended up with two branches with the same name after a branch renaming. By removing one of them in .git/refs/heads it all went back to normal.

like image 3
Michael Schmid Avatar answered Nov 12 '22 23:11

Michael Schmid