是否可以通过 git 别名覆盖 git 命令?
我的 ~/.gitconfig 是:
[alias]
commit = "!sh commit.sh"
但是,当我输入 git commit 时,不会调用脚本。
是否可能,或者我必须使用另一个别名?
my ~/.gitconfig is:
[alias]
commit = "!sh commit.sh"
However, when I type git commit, script is not called.
Is it possible, or I have to use another alias name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是不可能的
这是来自我的 git.git 克隆:
所以这是不可能的。 (
handle_internal_command
如果找到该命令,则调用exit
)。您可以通过更改行的顺序并在找到别名时使
handle_alias
调用exit
来修复源代码中的此问题。It is NOT POSSIBLE
This is from my clone of git.git:
So its not possible. (
handle_internal_command
callsexit
if it finds the command).You could fix this in your sources by changing the order of the lines and making
handle_alias
callexit
if it finds the alias.我选择使用 bash 函数来解决这个问题。如果我调用 git clone ,它会将调用重定向到 git cl ,这是我的别名,并添加了一些开关。
I opted to solve this with a bash function. If I call
git clone
, it will redirect the call togit cl
, which is my alias with some added switches.正如已经提到的,不可能使用 git 别名来覆盖 git 命令。但是,可以使用 shell 别名覆盖 git 命令。对于任何 POSIXy shell(即不是 MS
cmd
),编写一个简单的可执行脚本来执行所需的修改行为并设置 shell 别名。在我的.bashrc
(Linux) 和.bash_profile
(Mac) 中,我有在我的
~/bin
文件夹中,我有一个名为 < code>my-git 检查第一个参数(即 git 命令)是否是clone
。它看起来基本上是这样的:我的可配置性更高一点,但你明白了。
As already mentioned, it is not possible to use a git alias to override a git command. However, it is possible to override a git command using a shell alias. For any POSIXy shell (i.e. not MS
cmd
), write a simple executable script that performs the desired modified behavior and set a shell alias. In my.bashrc
(Linux) and.bash_profile
(Mac) I haveIn my
~/bin
folder I have an executable Perl script calledmy-git
that checks if the first argument (i.e. the git command) isclone
. It looks essentially like this:Mine is a little more configurable, but you get the idea.
不仅不可能,而且 WONTFIX
2009 年 http://git.661346.n2.nabble.com/allowing-aliases-to-override-builtins-to-support-default-options-td2438491.html< /a>
滨野回复:
Not only not possible, but also WONTFIX
In 2009 http://git.661346.n2.nabble.com/allowing-aliases-to-override-builtins-to-support-default-options-td2438491.html
Hamano replies:
FWIW,我通过编写以下
~/bin/git
包装器解决了这个问题(好吧,“解决了它”...),它检查例如~/bin/git-clone
,并调用 that 而不是内置函数。[注意:我为任何“聪明”的 bash-isms 表示歉意,但是在您通过两个辅助函数之后 - 一个用于扩展符号链接,另一个用于在 $PATH 中搜索正在包装的可执行文件 - 实际的脚本本身只是三个Lines of Code™...所以我想我毕竟不后悔,呵呵!]
FWIW, I solved this (okay, "worked around it"...) by writing the following
~/bin/git
wrapper, which checks for, e.g.,~/bin/git-clone
, and calls that instead of the built-in.[NOTE: I apologize for any "clever" bash-isms, but after you get past the two helper functions — one to expand symlinks and one to search your $PATH for the executable being wrapped — the actual script itself is just Three Lines of Code™... So I guess I'm not sorry after all, hehe!]
这是另一种解决方法,它并不依赖于实际上覆盖任何内容,而是依赖于滥用自动完成。这感觉比包装 git 更危险、更透明。
由于我从不键入完整的命令名称,因此定义要覆盖的命令的前缀别名就足够了。
例如,要使用别名覆盖
git show-branch
,并且知道我通常输入git show-
,我定义了show-br< /code> 别名来自定义
show-branch
行为。对于OP的示例
git commit
我通常输入git com
所以git comm
将是正确的解决方法。这种策略有几个优点:
Here’s yet another workaround, that does not rely on actually overriding anything, but relies on abusing autocomplete instead. This feels less dangerous and more transparent than wrapping git.
As I never type the full command names, it is enough to define an alias that is a prefix of the command to override.
For example, to override
git show-branch
with an alias, and knowing I usually typegit show-<Tab>
, I define theshow-br
alias to customise theshow-branch
behaviour.For OP’s example
git commit
I usually typegit com<Tab>
sogit comm
would be correct workaround.This strategy has several advantages: