I have a Git repository which contains a bunch of remote branches which no longer exist, but when I try to run git fetch -p
I get an error:
error: refs/remotes/origin/bad/branch does not point to a valid object!
I can't find any way to fix this. This branch doesn't exist as a separate file in .git/refs/remotes
(or anywhere else; I've searched with find
). Trying to delete it with git branch -D
doesn't work:
$ git branch -D origin/bad/branch
error: branch 'origin/bad/branch' not found.
$ git branch -D bad/branch
error: branch 'bad/branch' not found.
After comments below I tried adding the -r
option to the git branch -D
and got this error:
$ git branch -D -r origin/bad/branch
error: Couldn't look up commit object for 'refs/remotes/origin/bad/branch'
Is there some magic plumbing command I can use to get rid of this ref, short of just deleting the entire workspace which I would really prefer not to do?
This command will force-delete a remote-tracking branch:
But that didn’t work.
When the higher-level commands are too stubborn to do what you want, you might want to try a lower-level command:
Manually deleting
Note that manually editing things is generally not recommended. But it might be necessary. No warranties.
Refs are to my knowledge currently stored[1] in two possible places:
.git/refs
.git/packed-refs
(file)If it’s a file then
origin/bad/branch
is stored at.git/refs/origin/bad/branch
. Just deleting that file should be safe.If it’s a packed ref: open
.git/packes-refs
. I have not read any technical documentation for this file format but it just looks like a file on the form:In which case you might find the ref on a line like:
I guess you could just delete that line. But first do it in a backup of the whole repository (and working tree) just to be safe.
Notes