Reputation: 704
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
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
Reputation: 117
Solution which preserves your Git history and local changes.
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 .
git status
Upvotes: -1
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
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
Reputation: 2244
I have also encountered the same issue, and have resolved it as follows:
.git
folder of the cloned project..git
folder of the original project with the one that you've have copied.Why does this happen?
.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
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
Reputation: 1
Upvotes: 0
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
Reputation: 1739
The best solution for this problem:
'fatal: Failed to resolve HEAD as a valid ref'
Open the project on Eclipse IDE
Goto project Team menu and select Switch to, finally you can switch your current detached branch to your choice now.
As attached screenshot below:
Upvotes: -3
Reputation: 280
I had this problem after a Blue Screen of Death incident - so it was similar to what Sudip Bhandari said above.
.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..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).git rm -r --cached .
and git reset --hard
to clean up and then found all was back to normal again.Upvotes: 7
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
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