The currently accepted answer didn't help me because I didn't have a ref on the remote repo to delete - it was purely on my local! So if you're in that situation, here's what to do:
This is the issue I was facing:
$ git fetch origin
error: cannot lock ref 'refs/remotes/origin/fix/sub-branch':
'refs/remotes/origin/fix' exists; cannot create
'refs/remotes/origin/fix/sub-branch'
From <repo URL>
! [new branch] fix/sub-branch -> origin/fix/sub-branch
(unable to update local ref)
I tried the accepted answer's suggestion but got this:
$ git push origin :fix
error: unable to delete 'fix': remote ref does not exist
error: failed to push some refs to <repo URL>
So the ref didn't even exist on origin
- it was clearly just hanging around somewhere on my local repo. So I ran $ git remote show me
, which produced:
Remote branches:
...
refs/remotes/origin/fix stale (use 'git remote prune' to remove)
...
Which then made the solution clear:
$ git remote prune origin
Pruning origin
URL: <redacted>
* [pruned] origin/fix
With this, the problem disappeared:
$ git fetch origin
remote: Counting objects: 5, done.
remote: Total 5 (delta 2), reused 2 (delta 2), pack-reused 3
Unpacking objects: 100% (5/5), done.
From <repo URL>
* [new branch] fix/sub-branch -> origin/fix/sub-branch