问题
谁能解释发生了什么? [...] 我希望能够删除该分支,但 Git 不适合我。
通过运行
git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery
在 zsh 中,你没有创建任何分支。相反,您不小心定义了三个 shell 函数,称为 git、branch 和 SSLOC-201_Implement___str__,它们忽略了它们的参数(如果有的话)并且其主体是 _of_ProductSearchQuery。您可以通过调用名为 functions 的内置 zsh 命令来检查自己是否确实发生了这种情况,该命令列出了所有现有的 shell 函数:
$ functions
SSLOC-201_Implement___str__ () {
_of_ProductSearchQuery
}
branch () {
_of_ProductSearchQuery
}
git () {
_of_ProductSearchQuery
}
不幸的是,虽然其他两个 shell 函数没有问题,名为“git”的 shell 函数现在隐藏了 真正的git 命令!
$ which git
git () {
_of_ProductSearchQuery
}
# but the real "git" is a binary file that lives in /usr/local/bin/git (or some similar path)
因此,您随后会收到错误
command not found: _of_ProductSearchQuery
每当您尝试运行 Git 命令时,例如git log、git status 等(当然,假设不存在名为 _of_ProductSearchQuery 的命令)。
旁注
[...] 我得到同样的错误:
git:176: command not found: _of_ProductSearchQuery
(每次输入命令,git 后面的数字都会增加)
该数字仅对应于 HISTCMD 的值,这是一个保存的环境变量
[t]交互式 shell 中的当前历史事件编号,即导致读取 $HISTCMD 的命令的事件编号。
有关详细信息,请参阅zsh manual。
解决方案
我该如何恢复正常?
只需删除有问题的 shell 函数(以及您在使用它时意外创建的另外两个):
unset -f git
unset -f branch SSLOC-201_Implement___str__
那么一切都会好起来的。
如果unset 也被遮蔽了怎么办?!
Good question!我把你推荐给下面的Wumpus W. Wumbley's excellent comment。
分支命名提示
避免使用任何特殊的 shell 字符
是的,正如 cmets 中所指出的,括号是 Git 分支名称中的有效字符;您只需要适当地引用名称,例如
$ git branch 'foo()bar'
$ git branch
foo()bar
* master
$ git checkout 'foo()bar'
Switched to branch 'foo()bar'
但是,当用作命令行参数时,每次都需要引用此类名称,这应该会说服您在引用名称中避免使用括号。更一般地,您应该(尽可能)避免在 shell 中具有特殊含义的字符,以防止出现这样的意外。
使用简单的分支名称
无论如何,你应该保持你的分支名称简短而甜美。像
这样的长描述
SSLOC-201_Implement___str__()_of_ProductSearchQuery
属于提交消息,而不是分支名称。