Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a branch name contain the 'space' char?

Tags:

git

git-branch

I tried:

git branch "MyProj/bin/ ignored" 

and received:

fatal: 'MyProj/bin/ ignored' is not a valid branch name. 

The git-branch man page points to the git-check-ref-format man page to get the actual rules for a valid branch name.

Sure enough, the reason for the above fatal error appears to be the inclusion of a space character.

Any idea why, in this day and age, spaces are still excluded from a branch name (I would have expected it in ancient CVS, for example, but Git?)

What could be valid technical reasons for that?

like image 988
WinWin Avatar asked Jul 08 '11 01:07

WinWin


People also ask

Can you have spaces in branch names?

In Git, the git branch branch-name command is used to create a new branch called branch-name . Branches should be named something that describes the purpose of the branch. Note that branch names can't contain whitespace: new-feature and new_feature are valid branch names, but new feature is not.

Can a branch name have '/' in git?

Naming rules for refname: Git imposes the following rules on how references are named: They can include slash / for hierarchical (directory) grouping, but no slash-separated component can begin with a dot . or end with the sequence . lock .

Can branch names have underscore?

Use Hyphen or Slash as Separators My opinion is that hyphens make the name more comfortable to read, so it's a suitable separator in branch names. You can use slashes, hyphens, and underscores.


2 Answers

I do not know if you are going to find a pure, technical reason down at the bottom of this. However, I can offer that spaces tend to throw wrenches in all sorts of *nix utilities and filename processing, so it may have been to avoid accidentally doing anything wrong further down the line. After all, a git branch boils down to a file in the repo and this avoids dealing with spaces in that file's name (specifically, a branch is a file in .git/refs/heads/, as mentioned in the comment).

Mostly I would guess the reason is philosophical, and meant to keep things simple. Branch names are human-readable names that have no real reason to be complicated (and require typing two extra chars each time haha, to invoke the ghost of the sysadmin who has aliased every command to an indecipherable three letter combination). Otherwise known as the "why cd is not chdir" argument.

like image 79
shelhamer Avatar answered Oct 20 '22 22:10

shelhamer


old thread, but hey..
on a mac i use alt + space. it's gonna add an invisible character that will do the trick for you. mind: it's not a 'space', it's an invisible character. visually the same thing, but effectively not the same. 100% likely going to confuse the hell out of anyone else and definitely going to bring chaos everywhere, but hey, for the kicks.. why not? xD

git checkout -b US24024 Automated Tests - Profile A Switched to a new branch 'US24024 Automated Tests - Profile A' 
like image 29
joe Avatar answered Oct 20 '22 23:10

joe