【问题标题】:Why can't a branch name contain the 'hash' (#) char at the begining?为什么分支名称的开头不能包含“哈希”(#) 字符?
【发布时间】:2023-03-16 05:56:01
【问题描述】:

这个

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

产生错误

error: switch `b' requires a value

用反斜杠转义或用引号括起来会起作用

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

但这很奇怪

git branch #1-my-awesome-feature

不会产生任何错误,如果您检查它是否是用

创建的
git branch --all

没有分支。

如果 hash char 不在分支名称的第一个位置,则将创建分支

git branch feature-#1

正在执行git branch

feature-#1
* master

所以我的问题是如何在终端中“翻译”散列 (#) 字符以及为什么它在最初时不起作用?

谢谢!

【问题讨论】:

  • 平台很重要。这很可能适用于 Windows。

标签: git workflow naming-conventions git-branch git-checkout


【解决方案1】:

# 表示注释正在开始(至少在 linux shell 中)。所以

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

变成:

git checkout -b

并抛出 b 选项需要值的错误。

here 所示,您可以通过使用\ 转义# 或将名称放在单引号/双引号中来解决此问题:

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

【讨论】:

  • 有人可能还注意到# 一个有效的分支名称,它只需要被引用或转义。 git branch '#'git checkout -b '#' 工作得很好,尽管出于上面答案中列出的原因,以这种方式命名分支当然是不切实际的。
猜你喜欢
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 2018-02-18
  • 1970-01-01
相关资源
最近更新 更多