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

Viewed 59397

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?

5 Answers

Because using path names correctly in shell scripts is hard. From the linked git check-ref-format man page itself:

These rules make it easy for shell script based tools to parse reference names, pathname expansion by the shell when a reference name is used unquoted (by mistake), and also avoid ambiguities in certain reference name expressions (see gitrevisions(7)):

See also Filenames and Pathnames in Shell: How to do it Correctly:

The basic problem is that today most Unix-likes allow filenames to include almost any bytes. That includes newlines, tabs, the escape character (including escape sequences that can execute commands when displayed), other control characters, spaces (anywhere!), leading dashes (-), shell metacharacters, and byte sequences that aren’t legal UTF-8 strings.

...

However, this flaw in Unix-like kernels (allowing dangerous filenames) combines with additional weaknesses in the Bourne shell language, making it even more difficult in shell to correctly handle filenames and pathnames. I think shell is a reasonable language for short scripts, when properly used, but the excessive permissiveness of filenames turns easy tasks into easily-done-wrong tasks.

Related