294

I'm pretty sure I saw somewhere in a popular Git project the branches had a pattern like "feature/xyz".

However when I try to create a branch with the slash character, I get an error:

$ git branch labs/feature
error: unable to resolve reference refs/heads/labs/feature: Not a directory
fatal: Failed to lock ref for update: Not a directory

Same problem for (my initial attempt):

$ git checkout -b labs/feature

How does one create a branch in Git with the slash character?

  • 1
    FWIW anything before the slash will generate a directory under `.git/refs/heads` ie if you do `git checkout -b feature/123` then inside your `projectRootFolder/.git/refs/heads` directory you'll see a directory named: `feature` where inside that directory you'll see a branch named `123`. Later if you create another `feature/124` then inside the `feature` directory, you'll see a branch named `124` – mfaani Feb 14 '19 at 16:03

8 Answers8

289

Are you sure branch labs does not already exist (as in this thread)?

You can't have both a file, and a directory with the same name.

You're trying to get git to do basically this:

% cd .git/refs/heads
% ls -l
total 0
-rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 labs
-rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 master
% mkdir labs
mkdir: cannot create directory 'labs': File exists

You're getting the equivalent of the "cannot create directory" error.
When you have a branch with slashes in it, it gets stored as a directory hierarchy under .git/refs/heads.


Note that labs must not be an existing branch, as ddruganov points out in the comments:

 git switch -c 19023-commerce/19033-commerce-view 19023-commerce

 # Fails with:

 fatal: cannot lock ref 'refs/heads/19073-commerce-view/99999-test-branch': 
 'refs/heads/19073-commerce-view' exists; 
  cannot create 'refs/heads/19073-commerce-view/99999-test-branch'

As explained in "git push: refs/heads/my/subbranch exists, cannot create":

  • 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    Thanks for the in depth reply.. Interstingly I tried git branch foo/bar (which worked); then git branch -d foo/bar, but I see that the foo/ directory (now empty) still exists! EDIT: and it is replaced as soon as I do "git branch foo". All is well. –  Mar 26 '10 at 23:42
  • 1
    @faB: wicked... but not unexpected: you deleted bar (in the '`foo`' namespace), but not `foo` (which could serve as a namespace for other branch or be a branch itself) – VonC Mar 26 '10 at 23:44
  • This doesn’t really matter, but git doesn’t change its stance even when you call `pack-refs`, so it’s going out of its way to protect you from this. – Josh Lee Mar 27 '10 at 00:37
  • 51
    To summarize the answer: You can have slashes in branch names. OP already had a `labs` branch and tried to create `labs/feature`, which git balked at. – duozmo Jun 23 '14 at 17:06
  • @duozmo so whats the solution? how do i specify that i branched from some specific reference branch? – ddruganov Oct 28 '21 at 07:36
  • @ddruganov Simply by making sure you don't have a file which is part of the branch you are trying to create. – VonC Oct 28 '21 at 08:28
  • @VonC in my case I didnt have any files with such names; im talking about branching from "foo" to "foo/bar" which apparently is not possible – ddruganov Oct 29 '21 at 06:06
  • @ddruganov What version of Git are you using, on which OS? What exact command did you type, and what was its output? – VonC Oct 29 '21 at 06:10
  • @VonC git 2.33, macos 11.5.1; git checkout -b 19023-commerce/19033-commerce-view; the output was the same as in this topic; the branch 19023-commerce already exists by the way – ddruganov Oct 29 '21 at 07:55
  • @ddruganov Try `git switch -c 19023-commerce/19033-commerce-view 19023-commerce` – VonC Oct 29 '21 at 07:56
  • @VonC doesnt work, same error message; 'refs/heads/19023-commerce' exists – ddruganov Oct 29 '21 at 08:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238686/discussion-between-vonc-and-ddruganov). – VonC Oct 29 '21 at 08:26
134

It is possible to have hierarchical branch names (branch names with slash). For example in my repository I have such branch(es). One caveat is that you can't have both branch 'foo' and branch 'foo/bar' in repository.

Your problem is not with creating branch with slash in name.

$ git branch foo/bar
error: unable to resolve reference refs/heads/labs/feature: Not a directory
fatal: Failed to lock ref for update: Not a directory

The above error message talks about 'labs/feature' branch, not 'foo/bar' (unless it is a mistake in copy'n'paste, i.e you edited parts of session). What is the result of git branch or git rev-parse --symbolic-full-name HEAD?

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 2
    Thanks, sorry about the confusion, I first wrote a foo/bar example, but paste error message from my actual test. Will not do it again :) And sorry also for my mistake, indeed I had a "labs" branch already. –  Mar 26 '10 at 23:45
62

I forgot that I had already an unused labs branch. Deleting it solved my problem:

git branch -d labs
git checkout -b labs/feature

Explanation:

Each name can only be a parent branch or a normal branch, not both. Thats why the branches labs and labs/feature can't exists both at the same time.

The reason: Branches are stored in the file system and there you also can't have a file labs and a directory labs at the same level.

flori
  • 14,339
  • 4
  • 56
  • 63
39

Sometimes that problem occurs if you already have a branch with the base name.

I tried this:

git checkout -b features/aName origin/features/aName

Unfortunately, I already had a branch named features, and I got the exception of the question asker.

Removing the branch features resolved the problem, the above command worked.

Michael
  • 8,362
  • 6
  • 61
  • 88
elbkind
  • 531
  • 4
  • 5
1

Branch name is case sensitive and so if there are folders created in Azure by your release manager, folder names should match exactly. Learned it hard way raising it with manager.

1

In case someone has this issue where they're not able to checkout to a branch for eg: features/23 because it says branch features exists, but you cannot find it on Github - it might be that you have a local branch named features which is causing it.

Delete the local branch with git branch -D features and then you should be able to checkout with git checkout features/23 and it should work as expected.

nkhil
  • 1,452
  • 1
  • 18
  • 37
0

just had the same issue, but i could not find the conflicting branch anymore.

in my case the repo had and "foo" branch before, but not anymore and i tried to create and checkout "foo/bar" from remote. As i said "foo" did not exist anymore, but the issue persisted.

In the end, the branch "foo" was still in the .git/config file, after deleting it everything was alright :)

MonocroM
  • 1,039
  • 1
  • 11
  • 10
0

I do this from Visual Studio Git Changes

As previously mentioned. I made the misstake of creating a dev branch. main dev

thinking that later I could just call them dev/somebranchname.

Solution was to delete dev and then create the dev/somebranchname. Next dev branch will be called dev/nextbranchname. This integrates very well with Visual Studio where dev becomes a folder view and the branches line up after that

Tomas Hesse
  • 385
  • 3
  • 10