Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a branch name contain the 'hash' (#) char at the begining?

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!

like image 872
iveres Avatar asked Apr 05 '18 06:04

iveres


People also ask

Can a branch name have '/' in git?

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 . They must contain at least one / .

Can branch names have underscore?

Use hyphens as separators This is a little opinionated, but hyphens make for good separators in branch names. You could use an underscore, _ , too. The key is to be consistent, though. And that's it — just these three rules to keep in mind.


1 Answers

# 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'
like image 183
Varun Garg Avatar answered Sep 26 '22 20:09

Varun Garg