Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git unable to resolve references when pushing

Tags:

I am having an issue with Git. For unknown reasons, my master branch has somehow gotten corrupted. I have a local commit that I want to push up, but when I push, I get this:

git push origin master error: unable to resolve reference refs/remotes/origin/master: No such file or directory error: cannot lock the ref 'refs/remotes/origin/master'. Everything up-to-date 

I have seen the issue on other boards, but usually referring to pulls and not pushes. Nevertheless, I have tried their solutions, but to no avail:

  1. Tried amending to my current commit and pushing
  2. cleaned my git repository with git gc --prune=now
  3. Tried rm .git/refs/removes/origin/master

None have solved my issues. Any thoughts or ideas?

like image 857
cidthecoatrack Avatar asked May 20 '14 02:05

cidthecoatrack


1 Answers

(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 then git 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 -9ed earlier command look likely here, but I suspected an ambiguous ref and f-e-r is my first step for those.


like image 51
jthill Avatar answered Sep 20 '22 21:09

jthill