I randomly hit this today while trying to run Git garbage collect:
$ git gc
fatal: bad object refs/remotes/origin/HEAD
error: failed to run repack
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 prune
warning: symbolic ref is dangling: refs/remotes/origin/HEAD