定义具有相同名称的 git 别名以隐藏原始命令
我试图使用与现有命令相同的别名作为别名,以便别名隐藏原始命令(防止我从工作树中删除文件)。
[alias]
rm = rm --cached
diff = diff --color
不幸的是,这不起作用。有人知道解决方法吗? 谢谢。
编辑 设置 color.diff = true 会默认提供彩色输出。
I'm trying to use to use the same name for an alias as the existing command, so that the alias shadows the original command (preventing me from deleting files off the working tree).
[alias]
rm = rm --cached
diff = diff --color
Unfortunatly this is not working. Does anyone know a workaround?
Thanks.
Edit
Setting color.diff = true
gives colored output as default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于像
rm --cached
这样没有可配置选项的命令,最好的办法是创建一个不同名称的别名。例如:您可能已经明白了这一点,但 Git 别名无法隐藏现有的 Git 命令。来自
git-config
人页面:For commands like
rm --cached
that don't have configurable options, your best bet is to just make an alias named differently. For example:You may have already figured this out, but Git aliases cannot shadow existing Git commands. From the
git-config
man page:作为解决方法,您可以在 Bash 中定义别名来获得您想要的结果。这是我刚刚因为我的小烦恼而敲出来的东西 - 默认情况下“git add”并不冗长。 (并且没有它的配置设置)。
将其放入您的
~/.bash_profile
或~/.bash_rc
然后像平常一样调用它:
As a workaround, you can define aliases in Bash to get the result you want. Here's something I just knocked up for a pet peeve of mine - that 'git add' is not verbose by default. (And there's no config setting for it).
Put this in your
~/.bash_profile
or~/.bash_rc
Then just call it like normal:
Steve Bennett 的答案适用于简单的命令,但当您引用如下所示的参数时会中断:
将完整的参数列表保留为数组似乎可以工作:
现在命令成功:
Steve Bennett's answer works for simple commands, but breaks when you have quoted arguments like the following:
Preserving the full list of arguments as an array seems to work:
Now the command succeeds:
这是 Steve Bennett 翻译为 oh-my-zsh 的答案
等号需要用引号引起来。
这不起作用,因为它只是返回“git”是一个别名。
This is the answer of Steve Bennett translated for oh-my-zsh
The equals sign needs to be wrapped in quotes.
And which doesn't work as it just return that 'git' is an alias.