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

GitGit RemoteGit FetchGit Gc

Git Problem Overview


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

How do I deal with this?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

Thank god I found this https://makandracards.com/chris-4/54101-fixing-a-git-repo

fatal: bad object refs/remotes/origin/HEAD
error: failed to run repack

This may happen if upstream branches have been removed and your origin is pointing to it. You can confirm this by running:

cat .git/refs/remotes/origin/HEAD

If it is pointing to a branch that doesn't exist, running:

git remote set-head origin --auto

followed by

git gc

will fix it

Solution 5 - Git

Looks like your symbolic-refs might be broken... Try the replacing it with your default branch like this: For example, my default branch is master

$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master
$ git fetch --prune
$ git gc

That should fix it.

Solution 6 - Git

git update-ref -d [wrong reference here]

This will fix this issue.

For above issue use following code:

git update-ref -d 'refs/remotes/origin/HEAD'

In case you are getting error with .git like below:

error: bad ref for .git/logs/refs/remotes/origin/Dec/session-dynatrace-logs 6

You can copy the path starting from refs like below:

git update-ref -d 'refs/remotes/origin/Dec/session-dynatrace-logs 6'

Solution 7 - Git

I hit this error because the default branch was changed from master to main. I used a mix of info given by a few of the answers above to resolve it:

cat .git/refs/remotes/origin/HEAD

Returned:

ref: refs/remotes/origin/master

To fix it, I ran:

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.

Solution 8 - Git

If you're using git worktrees, make sure you're doing a

git worktree prune

before running

git gc

I had a worktree get corrupted and this seemed to do the trick after removing the corrupted worktree. git prune by itself didn't seem to work.

Solution 9 - Git

The cause of this for me was working in a compressed folder in Windows. When the folder was uncompressed, it corrupted the pack files, cascading other odd issues, such as not being able to prune nonexistent branches.

The only fix was to wipe out the working directory and clone the repo remote(s) again. Luckily, I could still push and pull updates to ensure nothing was lost. All is well now.

Solution 10 - Git

My problem occurred with a specific branch.
Apparently the reference file for branch was corrupted. I fixed it like that.

> git checkout main >
// I removed the file .git\refs\heads\branch_xpto >
git pull >
git checkout branch_xpto

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - GitpetrelharpView Answer on Stackoverflow
Solution 2 - GitWilQuView Answer on Stackoverflow
Solution 3 - GitTrentonView Answer on Stackoverflow
Solution 4 - GitThawinwats UggaraviseeView Answer on Stackoverflow
Solution 5 - GitConal Da CostaView Answer on Stackoverflow
Solution 6 - GitJoshua CleetusView Answer on Stackoverflow
Solution 7 - GitVirtua CreativeView Answer on Stackoverflow
Solution 8 - GitJeffPView Answer on Stackoverflow
Solution 9 - GitWill StrohlView Answer on Stackoverflow
Solution 10 - GitRafael PizaoView Answer on Stackoverflow