复杂的 Git 分支名称破坏了所有 Git 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32355540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Complex Git branch name broke all Git commands
提问by ruipacheco
I was trying to create a branch from master
with the following command,
我试图master
使用以下命令创建一个分支,
git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery
when Git suddenly stopped responding. I suspect the unescaped ()
are to blame, somehow. Now, whenever I try to run any Git command, I get the same error:
当 Git 突然停止响应时。()
不知何故,我怀疑未逃脱的人应该受到指责。现在,每当我尝试运行任何 Git 命令时,都会遇到相同的错误:
git:176: command not found: _of_ProductSearchQuery
with the number after git
increasing every time I type a command.
git
每次输入命令时数字都会增加。
Can anyone explain what happened? And how do I get back to normal? I'd like to delete that branch, but how can I do that?
谁能解释一下发生了什么?我如何恢复正常?我想删除那个分支,但是我该怎么做呢?
回答by jub0bs
Problem
问题
Can anyone explain what happened? [...] I'd love to be able to delete that branch, but Git won't work for me.
谁能解释一下发生了什么?[...] 我希望能够删除那个分支,但 Git 对我不起作用。
By running
通过跑步
git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery
in zsh, you did not create any branch. Instead, you accidentally defined three shell functions, called git
, branch
, and SSLOC-201_Implement___str__
, which ignore their parameters (if any) and whose body is _of_ProductSearchQuery
. You can check for yourself that this is indeed what happened, by invoking the builtin zsh command called functions
, which lists all existing shell functions:
在 zsh 中,您没有创建任何分支。相反,您意外地定义了三个shell 函数,称为git
、branch
和SSLOC-201_Implement___str__
,它们忽略了它们的参数(如果有的话)并且它们的主体是_of_ProductSearchQuery
。您可以通过调用名为 的内置 zsh 命令来检查自己是否确实发生了这种情况,该命令functions
列出了所有现有的 shell 函数:
$ functions
SSLOC-201_Implement___str__ () {
_of_ProductSearchQuery
}
branch () {
_of_ProductSearchQuery
}
git () {
_of_ProductSearchQuery
}
Unfortunately, although the other two shell functions are not problematic, the shell function called "git" now shadows the bona fidegit
command!
不幸的是,虽然其他两个 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)
Therefore, you will subsequently get the error
因此,您随后将收到错误
command not found: _of_ProductSearchQuery
whenever you attempt to run a Git command, e.g. git log
, git status
, etc. (assuming, of course, that no command called _of_ProductSearchQuery
exists).
每当您尝试运行 Git 命令时,例如git log
、git status
等(当然,假设不_of_ProductSearchQuery
存在被调用的命令)。
Side note
边注
[...] I get the same error:
git:176: command not found: _of_ProductSearchQuery
(with the number after
git
increasing every time I type a command)
[...] 我犯了同样的错误:
git:176: command not found: _of_ProductSearchQuery
(
git
每次键入命令时增加后的数字)
That number simply corresponds to the value of HISTCMD
, an environment variable that holds
该数字仅对应于 的值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.
[t] 交互式 shell 中的当前历史事件编号,即导致
$HISTCMD
读取的命令的事件编号。
See the zsh manualfor more details.
有关更多详细信息,请参阅zsh 手册。
Solution
解决方案
And how do I get back to normal?
我如何恢复正常?
Simply delete the problematic shell function (and the other two you created by accident, while you're at it):
只需删除有问题的 shell 函数(以及您在使用时意外创建的另外两个函数):
unset -f git
unset -f branch SSLOC-201_Implement___str__
Then everything should be fine.
那么一切都应该没问题。
What if unset
is shadowed also?!
如果unset
也有阴影怎么办?!
Good question! I refer you to Wumpus W. Wumbley's excellent commentbelow.
好问题!我向您推荐以下Wumpus W. Wumbley 的精彩评论。
Branch-naming tips
分支命名技巧
Avoid any special shell characters
避免任何特殊的 shell 字符
Yes, as pointed out in the comments, parentheses are valid characters in Git branch names; you just need to quote the name appropriately, e.g.
是的,正如评论中指出的,括号是 Git 分支名称中的有效字符;您只需要适当地引用名称,例如
$ git branch 'foo()bar'
$ git branch
foo()bar
* master
$ git checkout 'foo()bar'
Switched to branch 'foo()bar'
However, the need for quoting such names every single timewhen used as command-line arguments should convince you to eschew parentheses in reference names. More generally, you should (as much as possible) avoid characters that have a special meaning in shells, to prevent surprises like this one.
但是,每次用作命令行参数时都需要引用此类名称,这应该说服您避免在引用名称中使用括号。更一般地说,您应该(尽可能)避免在 shell 中具有特殊含义的字符,以防止出现这样的意外。
Use simple branch names
使用简单的分支名称
You should keep your branch names short and sweet anyway. Long descriptions like
无论如何,你应该保持你的分支名称简短而甜蜜。长描述如
SSLOC-201_Implement___str__()_of_ProductSearchQuery
SSLOC-201_Implement___str__()_of_ProductSearchQuery
belong in commit messages, not in branch names.
属于提交消息,而不是分支名称。