Question

I am moving from svn to git using git-svn, and some of my branches names started with '-'. Now with help form this question and this migration documentation I am able to pass parameters starting with '-'. but on creating branch git returns an error stating:

fatal: '-CLOSED-mybranch' is not a valid branch name.

I am using gitlab. The command that I am running to create branches is:

git for-each-ref refs/remotes | cut -d / -f 3- | grep -v @ | while read branchname; do git branch -- "$branchname" --"refs/remotes/$branchname"; git branch -r -d -- "$branchname"; done

This articles explains legal branch names and I think my branch name is Legal. Can anyone tell me where the problem could be?

PS: branch name not starting with '-' are successfully imported.

Was it helpful?

Solution

It doesn't seem you can create a branch which name starts with a dash:

$ git branch "-test"
error: unknown switch `e'

It apparently thinks that I'm trying to set the git branch option -t, and then gives an error on the following e.

It also doesn't work with this command:

$ git checkout -b "-test"
fatal: '-test' is not a valid branch name.

In this case it actually really says that -test is not a valid branch name. So it's probably best if you change your branch names so that they won't start with a dash.

OTHER TIPS

A short answer to your question is: unfortunately, this is not a legal branch name.

The article you referenced is incorrect (likely, simply because it is outdated). The rules for legal reference and branch names can be found in man 1 git-check-ref-format:

Git imposes the following rules on how references are named:

  1. They can include slash / for hierarchical (directory) grouping, but no slash-separated component can begin with a dot . or end with the sequence .lock.

  2. They must contain at least one /. This enforces the presence of a category like heads/, tags/ etc. but the actual names are not restricted. If the --allow-onelevel option is used, this rule is waived.

  3. They cannot have two consecutive dots .. anywhere.

  4. They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.

  5. They cannot have question-mark ?, asterisk *, or open bracket [ anywhere. See the --refspec-pattern option below for an exception to this rule.

  6. They cannot begin or end with a slash / or contain multiple consecutive slashes (see the --normalize option below for an exception to this rule)

  7. They cannot end with a dot ..

  8. They cannot contain a sequence @{.

  9. They cannot be the single character @.

  10. They cannot contain a \.

Note that these are the rules for references. There is a more strict set of rules for branches, they don’t seem to be listed in the manual, but it mentions that:

a dash may appear at the beginning of a ref component, but it is explicitly forbidden at the beginning of a branch name

which is exactly your case. In other words foo/bar/-test is valid, while -test is not.

Finally, you can sort of cheat by creating a branch with some other name, then going to .git/refs/heads and renaming it in there (with mv). You’ll then have this branch, you’ll be able to see it in git branch (unlike branches, whose names are illegal for a reference), but you still won’t be able to checkout it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top