How to handle git gc fatal: bad object refs/remotes/origin/HEAD error? How to handle git gc fatal: bad object refs/remotes/origin/HEAD error? git git

How to handle git gc fatal: bad object refs/remotes/origin/HEAD error?


I don't understand the ramifications of this, but as suggested in this thread, when I encountered this I just did

$ mv .git/refs/remotes/origin/HEAD /tmp

(keeping it around just in case) and then

$ git gc

worked without complaining; I haven't run into any problems.


The problem that I ran into (which is the same problem that @Stavarengo mentioned in this comment above) is that the default remote branch (develop in my case) had been deleted, but was still referenced in .git/refs/remotes/origin/HEAD.

Opening .git/refs/remotes/origin/HEAD in my editor showed this:

ref: refs/remotes/origin/develop

I carefully edited it to point at my new default branch and all was well:

ref: refs/remotes/origin/master

The clue that tipped me off was that running git prune showed this error:

> git prunewarning: symbolic ref is dangling: refs/remotes/origin/HEAD


After seeing Trenton’s answer, I looked at my .git/refs/remotes/origin/HEAD and saw that it was also pointing to an old branch that is now deleted.

But instead of editing the file myself, I tried Ryan’s solution:

git remote set-head origin --auto

It automatically set the file to the new branch, and git gc worked fine after that.