Creation of branch with / in the name fails
Platform Notice: Cloud, Server, and Data Center - This article applies equally to all platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
Creation of branch fails with the following error
fatal: cannot lock ref 'refs/heads/dev/feature1': 'refs/heads/dev' exists; cannot create 'refs/heads/dev/feature1'
Cause
This happens because of the way git stores pointers to the HEAD of each branch in the .git/refs/heads folder. In every git repository, there exists a .git/refs/heads
folder. It contains a text file for each branch on the repository that stores the commit hash for the tip of the branch. For example, if you have three branches in your repository named master, dev and feature, the files in .git/refs/heads
will look like this -
$ > ls .git/refs/heads
dev feature master
Each of these files corresponds to a branch.
Creation of a new branch called "feature/first-feature
" would mean creation of a file called "feature/first-feature
". However, this would not be allowed since a file called feature already exists and you cannot create a file and a directory with the same name.
Solution
To resolve this issue, there are two workarounds -
- Delete the branch called
feature
if you want to create other branches likefeature/feature-1
orfeature/first-feature
. Please make sure that this does not impact your code since this is a destructive action.
git push -d origin feature
git branch -d feature
The first command deletes the remote branch. Replace origin with your remote name if it is not origin.
The second command deletes the branch remotely. This command is not required if the branch has not been pushed yet.
- Rename your local and remote branch from
feature
tofeature/feature-1
. Name the subsequent features asfeature/feature-2
and so on. A branch called justfeature
cannot exist.
git branch -m feature feature/feature-1
git push -d origin feature
git branch --unset-upstream feature/feature-1
git push origin feature/feature-1
git push origin -u feature/feature-1