为什么 `~` 波浪号不能成为有效 Git 分支名称的一部分?

Why cannot the `~` tilde sign be part of a valid Git branch name?

我试图用 ~ 波浪号来表示贡献用户的 Git 分支,类似于在类 UNIX 操作系统上用来指代主文件夹的符号,但是 Git 阻止我这样做,因为它会中止分支创建并显示错误消息 fatal: '~' is not a valid branch name. The branch names from contributors should look like ~alice/feature/foo, ~bob/bugfix/bar etc.

None 以下命令适用于 Git 版本 2.14.1:

$ git checkout -b '~'
fatal: '~' is not a valid branch name.
$ git checkout -b '~bar'
fatal: '~bar' is not a valid branch name.
$ git checkout -b 'foo~'
fatal: 'foo~' is not a valid branch name.
$ git checkout -b 'foo~bar'
fatal: 'foo~bar' is not a valid branch name.

目前,作为替代表示,我决定将 @ at 符号作为我的通用前缀。但是我正在重新考虑将 - 连字符和 _ 下划线符号作为单词分隔符以外的相当不寻常的字符作为我的分支名称的一部分的决定,因为 [=27= 可能施加的限制] 本身或底层文件系统 Git 存储库恰好是例如签出并存储在。

The rules for reference names are given in the git check-ref-format manual page。波浪号 ~、克拉号 ^ 和冒号 : 字符是禁止的,因为它们是解析引用时的句法项:master~3 表示从提交开始倒数三个第一个父代master 点,例如,HEAD:path/to/blob 是当前提交中的 文件 (blob)。

A git 引用(标签或分支)禁止包含波浪号(~),因为该字符用于引用 parent 提交:

  • ref~ref~1 的 shorthand,表示 ref 的第一个 parent;
  • ref~2表示ref的第二个parent;
  • ...

查看 Paul Boxley 的 Git caret and tilde 文章,快速总结 git 语法中波浪号和插入符 (^) 的作用。

最后,您会在 official documentation.

中找到所有关于 git 引用提交语法的文档