分支名称中哪些字符是非法的?
分支名称中哪些字符是非法的?
Which characters are illegal within a branch name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
分支名称中哪些字符是非法的?
Which characters are illegal within a branch name?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
另外,如果您考虑使用美元符号
$
字符,则必须小心。gitbranch pew$ign
将创建pew
。为了创建一个包含
$
的分支,整个名称应该用 引号 括起来,使其成为字符串文字:gitbranch 'pew$ign'
。理想情况下,您应该避免使用任何符号。As an addition, care must be taken if you consider to use the dollar sign
$
character.git branch pew$ign
will createpew
.In order to create a branch that has
$
within it the whole name should be wrapped in quotes that make it a string literal:git branch 'pew$ign'
. Ideally you should avoid to use the symbol whatsoever.要完成 Manoj Govindan 的主要答案:
@
是有效的分支名称(在 git 版本git version 2.24.3 (Apple Git-128)
上)HEAD
不是有效的分支名称(显然是队长!)To complete the main answer by Manoj Govindan :
@
is a valid branch name (on git versiongit version 2.24.3 (Apple Git-128)
)HEAD
is not a valid branch name (by captain obvious!)接受的答案和 手册页 已经解释了哪些规则适用于 Git 分支名称。
在Git源代码中,通过 refname_disposition 数组来确定如何处理
refnames
中的各种字符。数组中的索引对应于 ASCII 代码,并且值指示如何处理 ASCII 字符。
由于
4
表示分支名称中不允许使用相应的 ASCII 字符,因此有 39 个不允许的字符。不允许的字符是 ASCII 控制字符(ASCII 代码 < 32)、可打印字符
: ? [ \ ^ ~
和空格/制表符。另外 3 个字符需要满足一些条件(请参阅文档注释):
.
:禁止使用两个连续的点。{
:禁止使用子字符串@{
。*
:除非设置REFNAME_REFSPEC_PATTERN
,否则拒绝。空字节终止分支名称,并且
/
为分支创建新的目录层次结构。因此,分支名称不能以斜杠结尾。
例如
git checkout -b 'a/b/c'
会在.git/refs/heads
下创建相应的目录结构,注意
UTF-8 字符可以在分支名称中使用:
The accepted answer and the man page already explain which rules apply to Git branch names.
In the Git source code, the refname_disposition array is used to determine how to handle various characters in
refnames
.The indexes in the array correspond to ASCII codes and the values indicate how the ASCII characters are handled.
Since
4
means that the corresponding ASCII character is not allowed inside branch names, there are 39 disallowed characters.The disallowed characters are ASCII control characters (ASCII codes < 32), the printable characters
: ? [ \ ^ ~
and the whitespace/tab character.3 more characters require some conditions to be met (see doc comment):
.
: Two subsequent dots are forbidden.{
: The substring@{
is forbidden.*
: Reject unlessREFNAME_REFSPEC_PATTERN
is set.The null byte terminates the branch name and
/
creates a new directory hierarchy for the branch.Therefore, branch names cannot end with a slash.
For example
git checkout -b 'a/b/c'
will create the corresponding directory structure under.git/refs/heads
Note that
UTF-8
characters can be used in branch names:refname的命名规则:
最重要的是,分支名称的附加规则:
感谢 Jakub Narębski,git check-ref-format 的“https://git-scm.com/docs/git-check-ref-format” rel="noreferrer">手册页 有更多详细信息。
Naming rules for refname:
On top of that, additional rule for branch name:
Thanks to Jakub Narębski, the man page for
git check-ref-format
has more details.