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

88,089

Solution 1

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

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

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

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

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.

Share:
88,089

Related videos on Youtube

Ryan
Author by

Ryan

Updated on July 08, 2022

Comments

  • Ryan
    Ryan almost 2 years

    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?

  • Stavarengo
    Stavarengo over 6 years
    It worked for me and I think I got into this problem because I changed the default branch from master to another one called develop. Days before I change it back from develop to master and I deleted the old default branch develop, but in my working directory, the file .git/refs/remotes/origin/HEAD was still pointing to refs/remotes/origin/develop which no longer exists. In this situation removing the file did work.
  • Sven Malvik
    Sven Malvik almost 6 years
    git prune worked for me, a way to delete data that has accumulated in Git but is not being referenced by anything useful.
  • David Rauca
    David Rauca almost 6 years
    Executing them solved my problem: $ mv .git/refs/remotes/origin/HEAD /tmp $ git gc git prune
  • Dan Carlstedt
    Dan Carlstedt over 5 years
    That was my fix as well
  • jmancherje
    jmancherje over 5 years
    This was my exact solution. Our team recently changed from using a default branch develop to master as well
  • Ivan Perez
    Ivan Perez almost 5 years
    I suspect the best way would be @WilQu's answer (stackoverflow.com/a/49944297/660339). Can anyone confirm this?
  • Vino
    Vino almost 5 years
    removing those file out of .git folder, than git gc worked for me
  • Lorenzo Solano Martinez
    Lorenzo Solano Martinez almost 5 years
    @Ryan you should check if this work for you, for me solved the problem.
  • Devy
    Devy over 4 years
    Yep, this works for me - as I was in the exact same scenario. git remote set-head $REMOTE --auto in my case, $REMOTE is the remote alias, not the default "origin", because I have multiple remotes setup.
  • L. J.
    L. J. over 4 years
    In my case git gc showed several files as fatal: bad object .... I moved each of them away into /tmp. Then, git gc worked and all was ok.
  • amcvitty
    amcvitty over 3 years
    I just did nearly this and it worked. Here's what I actually ran git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master; git fetch --prune; git prune; git gc;
  • Eli Nathan
    Eli Nathan about 3 years
    I had to run git gc after this to get everything working again but this fixed it!
  • mochadwi
    mochadwi about 3 years
    this fixed my issue as well :)))) thanks a lot!
  • Cauchy Schwarz
    Cauchy Schwarz about 3 years
    This fixes error cannot update the ref 'refs/remotes/origin/mybranch': unable to create directory for '.git/logs/refs/remotes/origin/mybranch': No such file or directory
  • 2540625
    2540625 over 2 years
    This gives me error: refusing to update ref with bad name '.git/logs/HEAD 2'.
  • Lex Scarisbrick
    Lex Scarisbrick over 2 years
    this worked for me with git version 2.33.0
  • BigMan73
    BigMan73 about 2 years
    Very helpful, thanks. This is a common issue when switching the head branch from master to main, as seen lately with many projects due to removing non inclusive terminology
  • mokagio
    mokagio almost 2 years
    git remote set-head origin --auto && git gc for convenient copy-paste in one go :)
  • Joshua Cleetus
    Joshua Cleetus almost 2 years
    make sure the path is correct
  • saran3h
    saran3h almost 2 years
    I got mv: cannot stat .git/refs/remotes/origin/HEAD: No such file or directory
  • andrew lorien
    andrew lorien almost 2 years
    The error also instructed "Please correct the root cause and remove .git/gc.log". This solution worked for me, only after removing that file.