复杂的Git分支名称打破了所有的Git命令 [英] Complex Git branch name broke all Git commands
问题描述
我尝试使用以下命令从 master
创建一个分支,
<$当Git突然停止响应时,c $ c> git branch SSLOC-201_Implement ___ str __()_ of_ProductSearchQuery
不知怎的,我怀疑非转义的()
是有责任的。现在,每当我尝试运行任何Git命令时,我都会得到相同的错误:
git:176:找不到命令:_of_ProductSearchQuery
随着 git
每次增加我输入一个命令。
任何人都可以解释发生了什么吗?如何恢复正常?
问题
我想删除那个分支,但我该怎么做?
任何人都可以解释发生了什么? [...]我希望能够删除该分支,但Git不适合我。
通过
git分支SSLOC-201_Implement ___ str __()_ of_ProductSearchQuery
zsh中的
,您没有创建任何分支。相反,您意外地定义了三个 shell函数,名为 git
,分支
和 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
}
#但真正的git是位于/ usr / local / bin / git(或某些类似路径)的二进制文件
因此,您随后会收到ge t错误
命令未找到:_of_ProductSearchQuery
每当你尝试运行一个Git命令时,例如 git log
, git status
等等(当然,假设没有叫
$ b 附注
[/ b>
git:176:找不到命令:_of_ProductSearchQuery
code>(每当我输入一个命令时,
git
>之后的数字会增加)
这个数字只对应于 HISTCMD
的值,一个环境变量持有
当前在交互式shell中的历史事件编号,换句话说就是导致<$ c的命令的事件编号请阅读
$ HISTCMD 。 zsh manual 获取更多详情。
解决方案
我该如何恢复正常?
有问题的shell函数(而另外两个是你在创建时偶然创建的):
unset -f git
unset -f branch SSLOC-201_Implement ___ str__
然后一切都应该是好的。
如果
unset
也被阴影化了怎么办?!
好问题!我推荐你 Wumpus W. Wumbley的优秀评论 a> below。
分支命名提示
避免任何特殊的外壳字符
是的,正如注释中指出的那样,括号是Git分支名称中的有效字符;你只需要适当地引用这个名字,比如
$ git branch'foo()bar'
$ git branch $ b $ f foo()bar
* master
$ git checkout'foo()bar'
转换到分支'foo()bar'
但是,当用作命令行时,每次都需要引用这些名称参数应该说服你避开引用名称中的括号。更一般地说,你应该(尽可能)避免在shell中有特殊含义的字符,以防止这样的惊喜。
使用简单的分支名称
无论如何,你应该保持你的分行名字短而甜。
$ b
SSLOC-201_Implement ___ str __()_ of_ProductSearchQuery
属于提交消息,不属于分支名称。
I was trying to create a branch from
master
with the following command,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:176: command not found: _of_ProductSearchQuery
with the number after
git
increasing every time I type a command.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?
解决方案Problem
Can anyone explain what happened? [...] I'd love to be able to delete that branch, but Git won't work for me.
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
, andSSLOC-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 calledfunctions
, which lists all existing shell functions:$ 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 fide
git
command!$ 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).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)That number simply corresponds to the value of
HISTCMD
, an environment variable that holds[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.See the zsh manual for more details.
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):
unset -f git unset -f branch SSLOC-201_Implement___str__
Then everything should be fine.
What if
unset
is shadowed also?!Good question! I refer you to Wumpus W. Wumbley's excellent comment below.
Branch-naming tips
Avoid any special shell characters
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 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 time when 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.
Use simple branch names
You should keep your branch names short and sweet anyway. Long descriptions like
SSLOC-201_Implement___str__()_of_ProductSearchQuery
belong in commit messages, not in branch names.
这篇关于复杂的Git分支名称打破了所有的Git命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!