43

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.

10
  • what is the output of git branch -v ?
    – Flows
    Commented Jun 2, 2016 at 14:46
  • hi @Flows its giving same "fatal: Failed to resolve HEAD as a valid ref." if I try git log then its giving me fatal: your current branch appears to be broken
    – idurvesh
    Commented Jun 2, 2016 at 14:46
  • Looking around internet, I found it may be due to permission access. Could you give 777 permissions to the .git folder recursively ?
    – Flows
    Commented Jun 2, 2016 at 14:55
  • I think 777 command is for remote directory? My problem is locally...getting this error when I do commit....
    – idurvesh
    Commented Jun 2, 2016 at 15:01
  • I know the problem is usually on remote repository but you can try that in your local repository too.
    – Flows
    Commented Jun 2, 2016 at 15:22

13 Answers 13

76

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.

6
  • 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
    Commented Mar 5, 2017 at 21:06
  • where can I find the .git folder? Commented May 22, 2019 at 4:28
  • @Andrea: Go to to your application directory and type ls -a. you'll find one. Commented May 22, 2019 at 7:51
  • @PraveshKhatri are you cloning from origin?
    – Jacques
    Commented Oct 3, 2022 at 8:09
  • Is there any risk of losing files/changes using this approach?
    – Jacques
    Commented Oct 3, 2022 at 8:29
11

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!

3
  • 1
    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
    Commented Jan 22, 2021 at 20:09
  • my guy you deserve an upvote
    – da coconut
    Commented Sep 11, 2022 at 11:27
  • This is exactly what I was looking for. I had the same issue where my system, unfortunately, crashed when I tried to save changes in one of the files on my working branch. Also, I had 10s of other WIP branches which I didn't want to lose. Thanks a lot! Commented Oct 18, 2023 at 8:12
6

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

6

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.
2
  • I just did the second step above and all was fixed.
    – daveywc
    Commented Sep 14, 2022 at 6:49
  • This is exactly a proper answer. Sad that it's not in the top of the list( Commented Jun 26 at 20:01
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.

0

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

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

0

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

0

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

-1

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

-1

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

1
  • The user didn't mention Eclipse.
    – mfisher91
    Commented Jan 22, 2021 at 20:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.