Can't create branches with slashes in Git if left part exists and commits also exists

Viewed 1273

I was using branches with slashes for some time, but suddenly found that I can't create them in some circumstances

$ git init
Initialized empty Git repository in /home/dimskraft/tests/git-slash-branch/.git/

$ git checkout -b my-branch
Switched to a new branch 'my-branch'

$ git status
On branch my-branch

No commits yet

nothing to commit (create/copy files and use "git add" to track)

$ git checkout -b my-branch/sub-branch
Switched to a new branch 'my-branch/sub-branch'

$ touch myfile.txt
$ git add myfile.txt 
$ git commit -m "added myfile.txt"
[my-branch/sub-branch (root-commit) 9be5cc5] added myfile.txt
...

$ git checkout -b my-branch/sub-branch/aaa
fatal: cannot lock ref 'refs/heads/my-branch/sub-branch/aaa': 'refs/heads/my-branch/sub-branch' exists; cannot create 'refs/heads/my-branch/sub-branch/aaa'

$ git status
On branch my-branch/sub-branch
nothing to commit, working tree clean

Why and is it possible to overcome?

1 Answers

Because a branch named a/b/c is stored in a file named refs/heads/a/b/c, which means that refs/heads/a/b is a directory. But if there is also a branch named a/b, then refs/heads/a/b must be a file, storing that branch.

Both things can't be true — at least on the systems Git runs on, you can only have one object on a filesystem with a given name, and it can't be both a directory and a file. So you can't have one branch with a name that is a prefix of another branch's name, when the first character they don't share is a slash.

Related