Using the slash character in Git branch name

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.

Leave a Comment