idurvesh
idurvesh

Reputation: 704

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 it's not working

Also tried

git commit

it's not working either from below the sources.

Git 'fatal: No such ref: HEAD'

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

Please help. All my commit history is also gone.

Upvotes: 47

Views: 86028

Answers (13)

LCoelho
LCoelho

Reputation: 3041

In my case, I was working with submodules and did not commit my changes yet. After commit, it starts to behave normal.

Upvotes: 0

dehidehidehi
dehidehidehi

Reputation: 117

Solution which preserves your Git history and local changes.

  • delete your .git folder
  • open command line
  • git init
  • git add origin [REMOTE_REPOSITORY_URL]
  • git origin fetch
  • git reset --soft origin/[BRANCH_NAME] -> this forces your local branch to be exactly like the remote branch except it will not overwrite your currently uncommitted changes.
  • git add .
  • Check results with git status

Upvotes: -1

Derek
Derek

Reputation: 9

Another solution for those who don't want to work on a new folder or clone again, just delete the .git folder, for windows users it may not show in the file explorer and deleting from cmd may be a problem, i dont know how to go about that. For me i use WSL so i can just switch between linux and windows. Type rm -fr .git to delete it. then start out like a new git repo with git init, git remote add origin, git branch -m (to what it was), git add ., git commit, git push origin branchName --force (it will raise an error if you don't force) then you will be back to speed

Upvotes: -1

MATCH
MATCH

Reputation: 102

In my case I just needed to do a git push in the main folder. I used git bash for this.

Upvotes: 0

Pravesh Khatri
Pravesh Khatri

Reputation: 2244

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.

Upvotes: 81

Rocoder
Rocoder

Reputation: 1103

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

Upvotes: 0

user15878180
user15878180

Reputation: 1

  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.

Upvotes: 0

Amit Jain
Amit Jain

Reputation: 127

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

Upvotes: 0

Yasiel Cabrera
Yasiel Cabrera

Reputation: 598

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!

Upvotes: 13

Chiranjhivi Ghimire
Chiranjhivi Ghimire

Reputation: 1739

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

Upvotes: -3

Oscar Bravo
Oscar Bravo

Reputation: 280

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.

Upvotes: 7

Michael Schmid
Michael Schmid

Reputation: 4997

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.

Upvotes: 3

hrvoj3e
hrvoj3e

Reputation: 2754

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

Upvotes: 7

Related Questions