复杂 Git 分支名称破坏了所有 Git 命令

Complex Git branch name broke all Git commands

我试图使用以下命令从 master 创建一个分支,

git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery

当 Git 突然停止响应时。我怀疑未转义的 () 是罪魁祸首。现在,每当我尝试 运行 任何 Git 命令时,我都会得到同样的错误:

git:176: command not found: _of_ProductSearchQuery

每输入一个命令,git后面的数字都会增加。

谁能解释一下发生了什么事?我该如何恢复正常?我想删除那个分支,但是我该怎么做呢?

问题

Can anyone explain what happened? [...] I'd love to be able to delete that branch, but Git won't work for me.

来自 运行宁

git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery

在 zsh 中,您没有创建任何分支。相反,您不小心定义了三个 shell 函数 ,分别称为 gitbranchSSLOC-201_Implement___str__,它们忽略了它们的参数(如果有的话) ) 并且 body 是 _of_ProductSearchQuery。您可以通过调用名为 functions 的内置 zsh 命令来检查自己是否确实发生了这种情况,该命令列出了所有现有的 shell 函数:

$ functions                                                     
SSLOC-201_Implement___str__ () {
    _of_ProductSearchQuery
}
branch () {
    _of_ProductSearchQuery
}
git () {
    _of_ProductSearchQuery
}

不幸的是,虽然其他两个 shell 函数没有问题,但 名为 "git" 的 shell 函数现在隐藏了 bona fide 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 loggit status 等(当然,假设不存在名为 _of_ProductSearchQuery 的命令)。

旁注

[...] I get the same error:

git:176: command not found: _of_ProductSearchQuery

(with the number after git increasing every time I type a command)

该数字仅对应于 HISTCMD 的值,这是一个包含

的环境变量

[t]he current history event number in an interactive shell, in other words the event number for the command that caused $HISTCMD to be read.

有关详细信息,请参阅 zsh manual

解决方案

And how do I get back to normal?

只需删除有问题的 shell 函数(以及您在使用时不小心创建的另外两个函数):

unset -f git
unset -f branch SSLOC-201_Implement___str__

那么一切都应该没问题。

如果 unset 也被隐藏了怎么办?!

Good question! I refer you to Wumpus W. Wumbley's excellent comment 下面。


Branch-naming小贴士

避免任何特殊的 shell 字符

是的,正如评论中所指出的,Git 分支名称中的括号是有效字符;您只需要适当地引用名称,例如

$ git branch 'foo()bar'
$ git branch
  foo()bar
* master
$ git checkout 'foo()bar'
Switched to branch 'foo()bar'

但是,当用作 command-line 参数时,需要每次 引用此类名称 应该说服您避免在引用名称中使用括号。更一般地说,您应该(尽可能)避免在 shell 中具有特殊含义的字符,以防止出现像这样的意外。

使用简单的分支名称

无论如何,你应该让你的分支名称简短而甜美。长描述如

SSLOC-201_Implement___str__()_of_ProductSearchQuery

属于提交消息,不属于分支名称。