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

Example fix from master as old branch to main as new branch

cat .git/refs/remotes/origin/HEAD

Returned:

ref: refs/remotes/origin/master

To fix it, run:

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

I ran this again to double-check:

cat .git/refs/remotes/origin/HEAD

Which returned:

ref: refs/remotes/origin/main

Then git gc and git prune worked just fine.


To see what happens I also tried:

git remote set-head origin --auto

Which returned:

origin/HEAD set to main

And it really solves the problem by identifying the ref automatically.