How to handle git gc fatal: bad object refs/remotes/origin/HEAD error?
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.
Related videos on Youtube
Ryan
Updated on July 08, 2022Comments
-
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 over 6 yearsIt worked for me and I think I got into this problem because I changed the default branch from
master
to another one calleddevelop
. Days before I change it back fromdevelop
tomaster
and I deleted the old default branchdevelop
, but in my working directory, the file.git/refs/remotes/origin/HEAD
was still pointing torefs/remotes/origin/develop
which no longer exists. In this situation removing the file did work. -
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 almost 6 yearsExecuting them solved my problem:
$ mv .git/refs/remotes/origin/HEAD /tmp
$ git gc
git prune
-
Dan Carlstedt over 5 yearsThat was my fix as well
-
jmancherje over 5 yearsThis was my exact solution. Our team recently changed from using a default branch develop to master as well
-
Ivan Perez almost 5 yearsI suspect the best way would be @WilQu's answer (stackoverflow.com/a/49944297/660339). Can anyone confirm this?
-
Vino almost 5 yearsremoving those file out of .git folder, than
git gc
worked for me -
Lorenzo Solano Martinez almost 5 years@Ryan you should check if this work for you, for me solved the problem.
-
Devy over 4 yearsYep, 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. over 4 yearsIn my case
git gc
showed several files asfatal: bad object ...
. I moved each of them away into/tmp
. Then,git gc
worked and all was ok. -
amcvitty over 3 yearsI 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 about 3 yearsI had to run
git gc
after this to get everything working again but this fixed it! -
mochadwi about 3 yearsthis fixed my issue as well :)))) thanks a lot!
-
Cauchy Schwarz about 3 yearsThis 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 over 2 yearsThis gives me
error: refusing to update ref with bad name '.git/logs/HEAD 2'
. -
Lex Scarisbrick over 2 yearsthis worked for me with git version 2.33.0
-
BigMan73 about 2 yearsVery helpful, thanks. This is a common issue when switching the head branch from
master
tomain
, as seen lately with many projects due to removing non inclusive terminology -
mokagio almost 2 years
git remote set-head origin --auto && git gc
for convenient copy-paste in one go :) -
Joshua Cleetus almost 2 yearsmake sure the path is correct
-
saran3h almost 2 yearsI got
mv: cannot stat .git/refs/remotes/origin/HEAD: No such file or directory
-
andrew lorien almost 2 yearsThe error also instructed "Please correct the root cause and remove .git/gc.log". This solution worked for me, only after removing that file.