Git unable to resolve references when pushing
(a worthwhile tl;dr from @NeTeInStEiN:
The nuclear option is rm -rf .git/refs/remotes/origin
, and that's what it took here)
edit: I've run across part of this behavior in one of my own repos, I can get git to reproduce the f-e-r failure without the rm .git/refs/remotes/origin/master
hack:
- clone a repo
- delete the origin's primary branch (the one its
HEAD
's attached to) - run
git fetch --prune
in the clone
The fetch will produce a dangling-ref warning, and git for-each-ref
will fail with a familiar message:
~/sandbox/20/buddy$ git fetch --prune
From /home/jthill/sandbox/20/source/.
x [deleted] (none) -> origin/master
(refs/remotes/origin/HEAD has become dangling)
~/sandbox/20/buddy$ git f-e-r
fatal: missing object 0000000000000000000000000000000000000000 for refs/remotes/origin/HEAD
but that doesn't break the push, I've tried with every setting of push.default
, nor does it break git update-ref -d
.
However, googling the push message did get me this:
I had just rebooted from a BSOD the other day [...] then git push. And that’s when I got a complaint about “Unable to resolve reference refs/remotes/origin/master…”. [...] So, I opened up the master file and it was full of spaces! Well, that’s no good. In order to fix it, I did this: [your
rm
, and thengit fetch
]
See comments above for the blow-by-blow, tl;dr is, because these were remote refs, which git fetch
completely refreshes, and because the damage was such that for-each-ref
and git update-ref
failed to work at all, the nuclear option rm -rf refs/remotes/origin; git fetch
was guaranteed to restore the remote properly.
In other circumstances, if there'd been no easy way to restore the damaged refs or for curiosity, find .git/refs/remotes/origin -type f
to check for locks or using reflogs (those files are in .git/logs
) to recover content would have helped but it wasn't necessary here. I think I missed a bet by not doing the find
first, *.lock
files from a kill -9
ed earlier command look likely here, but I suspected an ambiguous ref and f-e-r is my first step for those.
Remove the following directory from .git folder -> .git/refs/remotes/origin
This will remove all the remote branches and you need to fetch them again using -> git fetch. This should fix the issue.