254

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?

Paulo Mattos
  • 17,326
  • 10
  • 69
  • 79
Ryan
  • 3,325
  • 2
  • 17
  • 11

10 Answers10

278

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.

Manos Nikolaidis
  • 20,087
  • 12
  • 66
  • 75
petrelharp
  • 3,985
  • 1
  • 13
  • 16
  • 10
    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. – Stavarengo Aug 04 '17 at 15:21
  • 4
    `git prune` worked for me, a way to delete data that has accumulated in Git but is not being referenced by anything useful. – Sven Malvik Jun 15 '18 at 09:24
  • Executing them solved my problem: `$ mv .git/refs/remotes/origin/HEAD /tmp` `$ git gc` `git prune` – David Rauca Jul 19 '18 at 07:27
  • 6
    I suspect the best way would be @WilQu's answer (https://stackoverflow.com/a/49944297/660339). Can anyone confirm this? – Ivan Perez Apr 27 '19 at 16:18
  • removing those file out of .git folder, than `git gc` worked for me – Vino May 14 '19 at 13:58
  • @Ryan you should check if this work for you, for me solved the problem. – Lorenzo Solano Martinez Jul 01 '19 at 14:14
  • 1
    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. – L. J. Jan 01 '20 at 07:49
134

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.

WilQu
  • 6,353
  • 6
  • 29
  • 37
  • 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. – Devy Oct 28 '19 at 23:37
  • this worked for me with git version 2.33.0 – Lex Scarisbrick Sep 14 '21 at 15:40
110

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
Trenton
  • 1,328
  • 1
  • 9
  • 7
56

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

  • 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 – BigMan73 Feb 15 '22 at 15:54
27

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.

Conal Da Costa
  • 696
  • 7
  • 7
  • 2
    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;` – amcvitty Oct 16 '20 at 02:41
5
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'
Joshua Cleetus
  • 574
  • 5
  • 12
  • 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* – Cauchy Schwarz Apr 19 '21 at 04:05
  • This gives me `error: refusing to update ref with bad name '.git/logs/HEAD 2'`. – 2540625 Jul 28 '21 at 01:46
2

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.

Virtua Creative
  • 1,849
  • 1
  • 11
  • 17
1

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.

Will Strohl
  • 1,608
  • 1
  • 13
  • 31
1

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

Rafael Pizao
  • 482
  • 5
  • 18
0

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.

JeffP
  • 182
  • 12