git push: refs/heads/my/subbranch exists, cannot create

I was in a state when I couldn't even fetch because my repo had info about non-existing remote branches I didn't even had checked out. I solved it by running combination (thanks @torek) of:

  • git branch -r list local copies of remote branches
  • git ls-remote list remote branches
  • git fetch --prune origin update local copies of remote branches (this actually didn't help me)
  • git remote prune origin remove info about removed remote branches (this did)

It's not a folder that exists, it's a branch. (Well, there may be a folder/directory involved somewhere—or maybe not, as references get "packed" and stop existing as files within directories.)

  • If branch b exists, no branch named b/anything can be created.
  • Likewise, if branch dev/b exists, dev/b/c cannot be created.

This is a git internal limitation. In this particular case, remote origin has a branch named dev/sub (regardless of whether you have it or not, the important thing is whether the remote has it). In order to create, on origin, a branch named dev/sub/master, you must first delete the branch named dev/sub on origin:

git push origin :dev/sub

(Of course, deleting this branch may delete something important over there, so be sure you know what you are doing. Generally, you might want to git fetch origin first, capturing their dev/sub as your origin/dev/sub. You can then make a local branch named dev/renamed-sub pointing to the same commit, create dev/renamed-sub on the remote, delete the remote dev/sub, and then create dev/sub/master on the remote.)


If you can log in on the remote (the system that origin is hosted on), you can go into the repository over there and simply rename the local dev/sub branch. (Based on comments below, I suspect that there's a broken auto-deploy script over there as well, which probably should be fixed to only deploy "deployable" branches, rather than everything that gets pushed. But I am just guessing here.)


For me -->

Error =

fatal: cannot lock ref 'refs/heads/release/wl/2.3': 'refs/heads/release/wl' 
exists; cannot create 'refs/heads/release/wl/2.3'

Solution =

$~ git update-ref -d refs/heads/release/wl
$~ git checkout release/wl/2.3

Tags:

Git