34

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

Community
  • 1
  • 1
idurvesh
  • 564
  • 1
  • 5
  • 19

9 Answers9

50

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.

Pravesh Khatri
  • 1,904
  • 14
  • 20
  • 2
    This actually worked for me too, I had a repo that randomly could no longer find any branches (not even master), but nothing else I did (git checkout master, git checkout -, etc) worked. A solution as simple as recloning and then copying the git folder fixed it. This even let me keep my changes, so I didn't need to redo them. – CDerrig Mar 05 '17 at 21:06
  • where can I find the .git folder? – Andrey May 22 '19 at 04:28
  • @Andrea: Go to to your application directory and type `ls -a`. you'll find one. – Pravesh Khatri May 22 '19 at 07:51
5

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

hrvoj3e
  • 2,067
  • 1
  • 20
  • 19
5

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!

Yasiel Cabrera
  • 480
  • 3
  • 10
  • Sorry to say, but this issue actually made issues worse for me, now I see: `fatal: not a git repository (or any of the parent directories): .git` – mfisher91 Jan 22 '21 at 20:09
3

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.

Michael Schmid
  • 2,566
  • 19
  • 22
2

I had this problem after a Blue Screen of Death incident - so it was similar to what Sudip Bhandari said above.

  • I looked in .git/refs/heads/<mybranch> and found that the entry was corrupted (illegible). This file is supposed to contain the full commit-id of the HEAD branch.
  • I cloned a new repository and copied .git/refs/heads/<mybranch> from the new checkout over the corrupted one (I guess I could've just repaired it by pasting in a recent commit-id from stash or whatever).
  • back in the original repository, I did git rm -r --cached . and git reset --hard to clean up and then found all was back to normal again.
Oscar Bravo
  • 230
  • 4
  • 11
0
  1. Backup your existing .git folder.
  2. Clone your project from git repository into new folder.
  3. Copy .git from new project folder and replace your original .git folder.
Simas Joneliunas
  • 2,705
  • 15
  • 25
  • 32
0

WHY - fatal: Failed to resolve HEAD as a valid ref?

git is not able to figure out the reference and you may see this issue if the reference head is corrupted.

Suppose originally you checked out a feature branch out of the master branch and because of some failure you hit this issue. You can check the ref of the header file it should be corrupted or no value in it. Ideally, this file supposes to keep the commit hash value.

cat .git/refs/heads/<my-branch>

SOLUTION - Replacing the corrupted ref or ref value will solve the problem.

Goto .git/ref/heads/ and check if you already have some branch there and copy the commit hash value to your branch.

cat .git/refs/heads/<branch_with_commit_details> > .git/refs/heads/<my-branch>

OR you can clone the source code in another directory and copy the same hash value to my-branch

Rocoder
  • 1,043
  • 2
  • 15
  • 25
-1

Just remove the files under .git/refs/heads. It works for me

Amit Jain
  • 39
  • 3
-3

The best solution for this problem:

'fatal: Failed to resolve HEAD as a valid ref'

  1. Open the project on Eclipse IDE

  2. Goto project Team menu and select Switch to, finally you can switch your current detached branch to your choice now.

As attached screenshot below:

enter image description here

Chiranjhivi Ghimire
  • 1,683
  • 1
  • 16
  • 21