Creation of branch with / in the name fails

Still need help?

The Atlassian Community is here for you.

Ask the community

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 like feature/feature-1 or feature/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 to feature/feature-1. Name the subsequent features as feature/feature-2 and so on. A branch called just feature 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




Last modified on Dec 26, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.