334

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
  • 18,845
  • 10
  • 77
  • 85
Ryan
  • 4,425
  • 2
  • 23
  • 12

17 Answers17

354

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
  • 21,608
  • 12
  • 74
  • 82
petrelharp
  • 4,829
  • 1
  • 16
  • 17
  • 12
    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
  • 1
    Executing them solved my problem: `$ mv .git/refs/remotes/origin/HEAD /tmp` `$ git gc` `git prune` – David Rauca Jul 19 '18 at 07:27
  • 8
    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
  • 2
    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
  • I got `mv: cannot stat .git/refs/remotes/origin/HEAD: No such file or directory` – saran3h May 23 '22 at 08:28
  • 1
    In my case, this happened because of Dropbox adding files like `index (xyz's conflicted copy 2022-06-25)` and `ORIG_HEAD (xyz's conflicted copy 2022-06-25)` etc. to the `.git` folder and subfolders in `.git/refs/`. Removing those and running `git gc` seem to have worked. – passerby51 Jul 05 '22 at 18:27
  • This also worked for non HEAD ref errors. (you just have to move the correct branch name to some other folder) – Cool guy Jul 12 '22 at 10:17
  • In my case, I was getting this error even after the branch this error was attached to had been deleted. I ended up just deleting the ref fil and doing a git prune, and it worked. – Bad Programmer Jul 29 '22 at 17:27
  • I get: mv: cannot stat '.git/refs/remotes/origin/HEAD': No such file or directory – Nathan B Nov 29 '22 at 07:04
190

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
  • 7,131
  • 6
  • 30
  • 38
  • 1
    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
  • The error also instructed "Please correct the root cause and remove .git/gc.log". This solution worked for me, only after removing that file. – andrew lorien Jun 06 '22 at 07:01
120

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,438
  • 1
  • 10
  • 7
83

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

  • 4
    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
  • `git remote set-head origin --auto && git gc` for convenient copy-paste in one go :) – mokagio May 04 '22 at 03:47
29

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
  • 774
  • 7
  • 7
  • 4
    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
8
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
  • 624
  • 6
  • 14
8

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
  • 2,025
  • 1
  • 13
  • 18
4

rm .git/refs/remotes/origin/HEAD

git gc

3

The above solution partially worked for me because my folder had the "desktop.ini" files everywhere in the repository as it is hosted on Google Drive, including the “.git” folders where Git was storing its own data. Git expected every file in that folder to contain Git data, not Google Drive data, and it choked trying to interpret the desktop.ini file contents. To avoid this, make sure to include desktop.ini in .gitignore

I first deleted these files using a batch command on windows as follows:

  1. create a "delete.bat" file in the repository and add the following code to it

    del /s /q /f /a ".\desktop.ini"

  2. Open cmd and open the current folder

  3. run delete.bat by simply calling it in cmd

Now you should be able to run git remote set-head origin --auto

followed by git gc

3

If someone is getting this error

fatal: bad object refs/stash 2
error: https://github.com/Username/repository.git did not send all necessary objects

this is how I fixed

mv .git/refs/stash\ 2 /tmp
git gc
Diego Bianchi
  • 601
  • 5
  • 10
1

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
  • 324
  • 2
  • 13
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,646
  • 2
  • 15
  • 32
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
  • 742
  • 8
  • 21
1

I ran into the same issue, when I tried to pull from the origin branch, I got the following error:

fatal: bad object refs/remotes/origin/account

The solutions above didn't work for me for some reason. Kept getting this error

mv: cannot stat '.git/refs/remotes/origin/HEAD': No such file or directory

And running git gc gave this error:

error: bad ref for .git/logs/refs/remotes/origin/account
fatal: bad object refs/remotes/origin/account
fatal: failed to run repack

In my situation, the remote branch was pointing to a branch that didn't exist.

What fixed it for me was deleting the branch

git branch -D account

and also deleting it from .git/refs/remotes/origin/account

Everything working perfectly.

Victor Eke
  • 109
  • 6
1

What worked for me was to get into the folder itself in my pc cause I kept getting the error

No such file or directory

whenever I run

mv .git/refs/remotes/origin/HEAD /tmp 

$ git gc git prune

After opening the hidden files, you can do this by pressing cmd + shift + . on Mac or Windows then ref > remote > origin and delete the unnecessary files

Riziki
  • 51
  • 1
  • 5
0

In my I just deleted that particular refs: refs/heads/feat/implement-games. After that, I was already able to git pull origin master

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
0

I am not really sure how this occurs, but it seems that its being caused due to duplicates.

What worked for me was manually deleting duplicates. I navigated to each of the folders and manually removed the duplicates.

I had the below issue bad object refs/tags/v1.0.1 2

  1. Open the repo in a terminal
  2. Navigate to /tags using the cd command
  3. Run the ls command to verify the duplicate exists i.e. v1.0.1 2
  4. Run the rm command to remove the duplicate -> rm v1.0.1\ 2 (\ to handle the space)

Then try to git gc again. In my case, I had more duplicates in diff locations that I had to manually delete in the same manner, but once all were removed - everything work normally.

I hope this works for you!

Tushar
  • 1
  • 1