13

This one

git checkout -b #1-my-awesome-feature

creates error

error: switch `b' requires a value

escaping it with backslash or wrapping it in quotes will work

git checkout -b \#1-my-awesome-feature

but strange enough this

git branch #1-my-awesome-feature

will not produce any error and if you check if it is created with

git branch --all

there is no branch.

If hash char is not in the first position of the branch name, branch will be created.

git branch feature-#1

Executing git branch

feature-#1
* master

So my question is how hash (#) char is 'translated' in terminal and why it is not working when it is at first place?

Thanks!

3
  • Platform is important. This will most likely work on Windows. Commented Apr 5, 2018 at 8:33
  • @ThorbjørnRavnAndersen, Yes, and even in PowerShell, it appears that # will not start a comment in cases like above where the # is not preceded by a space character. So git branch feature-#1 works in PowerShell, but git branch #1-my-awesome-feature does not (need to escape the # with a backtick, or put full name in quotes). Commented Jun 20 at 8:13
  • Ah, this is the case in the shell bash as well, except it uses backslash (not backtick) for escaping a character. Commented Jun 20 at 8:22

1 Answer 1

28

# means a comment is starting (atleast in a linux shell). So

git checkout -b #1-my-awesome-feature

becomes:

git checkout -b

and throws error that b option requires a value.

As shown here, you can solve this by escaping the # with a \ or by putting the name in single/double quotes:

git checkout -b \#1-my-awesome-feature
git checkout -b "#1-my-awesome-feature"
git checkout -b '#1-my-awesome-feature'
1
  • 1
    One might also note that # is a valid branch name, it just needs to be quoted or escaped. git branch '#' or git checkout -b '#' works just fine, although it would of course be impractical to name branches this way for the reasons listed in the answer above.
    – jsageryd
    Commented Apr 5, 2018 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.