jokarl
jokarl

Reputation: 2245

Git branch named release cannot be deleted and prevents creating branches prefixed with release/

I cannot create branches prefixed with release:

$ git checkout master && git pull
$ git checkout -b release/0.7.6
$ git push --set-upstream origin release/0.7.6
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create pull request for release/0.7.6:
remote:   https://bitbucket.org/xxx/xxx/pull-requests/new?source=release/0.7.6&t=1
remote: 
To bitbucket.org:xxx/xxx.git
 * [new branch]      release/0.7.6 -> release/0.7.6
Branch 'release/0.7.6' set up to track remote branch 'release/0.7.6' from 'origin'.
error: update_ref failed for ref 'refs/remotes/origin/release/0.7.6': cannot lock ref 'refs/remotes/origin/release/0.7.6': 'refs/remotes/origin/release' exists; cannot create 'refs/remotes/origin/release/0.7.6'

When I look in the bitbucket interface, I can see the branch release/0.7.6 there, 0 commits before and 0 commits ahead of master.

Writing git fetch produces additional errors:

$ git fetch
error: cannot lock ref 'refs/remotes/origin/release/0.7.6': 'refs/remotes/origin/release' exists; cannot create 'refs/remotes/origin/release/0.7.6'
From bitbucket.org:xxx/xxx
 ! [new branch]      release/0.7.6 -> origin/release/0.7.6  (unable to update local ref)

When I write git checkout and use tab completion, I indeed get the suggestion that release would be something I can check out, but again it fails when I try to do that:

$ git checkout release
fatal: cannot lock ref 'refs/heads/release': 'refs/heads/release/0.7.6' exists; cannot create 'refs/heads/release

I can see there is something called release and it is listed in red, but I cannot delete it:

$ git branch -a | grep remotes/origin/release
  remotes/origin/release
$ git branch -D origin/release
error: branch 'origin/release' not found.
$ git branch -D release
error: branch 'release' not found.
$ git branch -D remotes/origin/release
error: branch 'remotes/origin/release' not found.

It can be checked out if I first remove my created release/0.7.6

$ git checkout release
Branch 'release' set up to track remote branch 'release' from 'origin'.
Switched to a new branch 'release'

What have I managed to do, and how can I solve it?

Update Our bitbucket branching model is traditional git flow:

enter image description here

The branch permissions have protected history and write on master, only allowed to merge with a PR.

I can't find any setting that would cause this.

Upvotes: 1

Views: 1134

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83557

TLDR; Branches with the pattern A/B exist as a file named B in a folder named A. If you already have another branch named A, you will get an error similar to what you see.

error: update_ref failed for ref 'refs/remotes/origin/release/0.7.6': cannot lock ref 'refs/remotes/origin/release/0.7.6': 'refs/remotes/origin/release' exists; cannot create 'refs/remotes/origin/release/0.7.6'

The error tells you that a branch named release already exists in your BitBucket repo. Under the hood this is stored in a file in .git/refs/remotes/origin/release. Now you are trying to push a branch named release/0.7.6 which will create a file in .git/refs/remotes/origin/release/0.7.6. Since the file release already exists, git cannot create a folder named release to store the 0.7.6 file in.

To fix the problem, first double check that you can remove the release branch. If you are sure this is safe, then you can delete it with git push origin :release.

Upvotes: 2

Related Questions