tags:

views:

309

answers:

3

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?

A: 

I could be wrong, but I thought that slashes only appeared in branch names when they related to a remote repo, for example origin/master.

crazyscot
It's perfectly possible - and indeed common - to create local branches with '/' in their names. It's a common way of grouping related branches in a 'namespace'.
Charles Bailey
Yeah to be honest that can be confusing when learning, but the naming can be useful too. Then again using slash or dash, I'm not sure if it matters beyond personal taste?
faB
+2  A: 

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
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.
faB
+3  A: 

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.

VonC
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.
faB
@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
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.
jleedev