简体   繁体   中英

How can I checkout a branch name having special character '-'?

I created a git branch name -new-br :

git checkout -b -new-br.

I switched back to parent branch.

Now I am unable to checkout -new-br .

git checkout -new-br.
getting error:
error: unknown switch `n'
usage: git checkout [options] <branch>
   or: git checkout [options] [<branch>] -- <file>...

How can I check this branch out?

Many Unix commands accept -- to mean "no more switches" and further arguments aren't then parsed as switches. See " What does “--” (double-dash) mean? " on this adjacent site .

So...

git checkout -- -new-br

...would probably suffice.

However... which version of Git are you using to create such a branch? Git version 1.8.4 tells me:

git checkout -b -new-br
fatal: '-new-br' is not a valid branch name.

Trying it another way...

git branch -- -new-br
fatal: '-new-br' is not a valid branch name

I suggest that you check the names of all your branches with:

git branch --list --all

Reference:

If you have an existing branch like that, you can rename it by changing the reference file for the branch:

mv .git/refs/heads/-new-br .git/refs/heads/new-br

After that, you can check it out using git checkout new-br .

This works! Add your branch name inside quotes

git checkout '<new-branch-name>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM