SteGriff

Blog

Next & Previous

Git - bad ref - failed to run repack

I was trying to git pull and it wouldn’t work. When I ran git gc, I got:

$ git gc --aggressive --prune=now
error: bad ref for .git/logs/refs/remotes/origin/something-beta
fatal: bad object refs/remotes/origin/something-beta
fatal: failed to run repack

None of the stack answers helped very much, so I figured, “git state is just a bunch of files, right? And it told me the paths. So I’ll just look at them and delete them :)”

This is on the basis that the nuclear option is to delete the local clone and check it out again from remote. My approach seems lighter than that and has the nuclear option as a fallback position.

I have two interesting errors. For some reason I tackled the second one first:

fatal: bad object

I navigated to the path it told me (.git/refs/remotes/origin/something-beta)

The file content was entirely nulls. I think some process (could be git, could be libgit2 in VS/Code) has died while writing this file, corrupting it. I deleted it.

error: bad ref

Well, the error message gives me the strong sense that this ref was trying to reference whatever was in the other corrupt file. So, I deleted .git/logs/refs/remotes/origin/something-beta as well.

Retry

I ran git gc and it completed happily.

Then I was able to git pull.

Have a Happy December! ❄