0

I have an existing branch called feature/story-30 ,

Now I am trying to create an another git branch called feature/story-30/Task-120 but somehow it is not letting me do that and throwing an error ,

fatal: cannot lock ref 'refs/heads/feature/story-30/Task-120': 'refs/heads/feature/story-30' exist; cannot create 'refs/heads/feature/story-30/Task-120

l.b.vasoya
  • 1,188
  • 8
  • 23
Akash
  • 848
  • 1
  • 11
  • 30
  • 1
    If you imagine that branches are actually files and folders, you are trying to create a file inside a file. Try removing `feature/story-30 first`? – evolutionxbox Jun 25 '20 at 14:21
  • https://stackoverflow.com/search?q=%5Bgit%5D+fatal%3A+cannot+lock+ref+exist+cannot+create – phd Jun 25 '20 at 14:28

3 Answers3

2

The branches are tracked using dirs and files.... normal files and directories inside .git. So the file for feature/story-30 already exists.... you are asking to use it as one directory now and that's not possible. It's like having a file called blah in your project and then try to create a directory called blah. That won't fly.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • but in windows file system, we can do that same. means can create a blah file and blah folder under the same parent folder. – Akash Jun 25 '20 at 16:50
  • 1
    ah... what? That sounds more like you are hiding the extensions of files and therefore you think that the file is blah, but it's actually blah.txt, for example. Check if that's the case with git bash. – eftshift0 Jun 25 '20 at 17:01
  • I see your point. as GIT created file does not hv extension so it would not let me create an another folder with same name. – Akash Jun 25 '20 at 20:00
2

A branch in git is just a text file containing a commit Id. This is located in .git/refs/heads. In your case there is a text file in .git/refs/heads/feature called story-30. Trying to create a branch called feature/story-30/Task-120 attempts to create sub folder in .git/refs/heads/feature called story-30, but this already exists as a text file, so the OS reports back to Git that it cannot create the directory. As a result, Git reports back to you that it cannot create the branch.

You will need to create a new branch with a different name.

git push: refs/heads/my/subbranch exists, cannot create is a similar question where this same error occurred when pushing a branch to another server, so be aware this can happen locally when creating a branch, and on the server when pushing a branch.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Hi Greg, that's where I got confused as in windows, I can create folder and a file with the same name and window does not complaine. Like "Parent -> child-folder" and "Parent -> child-folder.txt" so I was hoping, that git will follow the same. but apprently, as I tried creating an folder inside ".git/refs/heads/feature" with name "story-30", it would not let me do that saying that f"file exist with same name". – Akash Jun 25 '20 at 16:44
  • one more thing, if I need to create branches based on the sub-task in a user story, than what is Git naming convention says? As earlier, I use to create branch names based on the User-story number but in the current project, we need to create branches based on the subTask in a user-story. – Akash Jun 25 '20 at 16:47
  • child-folder and child-folder.txt are different names. File file as the file extension .txt. The text files that Git creates to represent a branch _do not have a file extension._ – Greg Burghardt Jun 25 '20 at 17:05
  • 1
    @Akash: there is no naming convention for branches that are sub tasks of a feature. My team creates branches using our usernames as a sub folder, and then "feature" or "topic" as sub folders for branches we all work in. So we would have a `topic/blog_post_comments` and I might have a branch for a task: `GregBurghardt/1234_insert_comment_into_db` – Greg Burghardt Jun 25 '20 at 17:07
  • Thank You for the clarification and example of your team. – Akash Jun 25 '20 at 20:02
1

You cannot create a branch with a slash in the name if the part before the slash already exists as a branch.

You can change the name of the branch to match Git naming, if that helps.