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

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.


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'

There is a possible workaround if you are desparate enough. There are a lot of space-like characters in the unicode set. But only U+0020 is the space that is disallowed. Take e.g. a non-breaking space and you can have a branch name with spaces. The main issue is that your keyboard likely has no key for that code point. I use the following script to work around that issue:

#!/bin/zsh
git co -b "${@// / }"

It simply replaces all spaces in the arguments with non-breaking spaces...

Tags:

Git

Git Branch