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
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'
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.
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.
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.
When all other answers fail, just restart your computer.
I encountered OP's error and also had Permission denied
on some .gitattribute
file in unrelated directory (no matter if run by admin or not). This helped me.
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