我的〜/ .gitconfig是:
[alias]
commit = "!sh commit.sh"
Run Code Online (Sandbox Code Playgroud)
但是,当我输入git commit时,不会调用脚本.
是否可能,或者我必须使用另一个别名?
alt*_*ive 43
这不可能
这是我的git.git的克隆:
static int run_argv(int *argcp, const char ***argv)
{
int done_alias = 0;
while (1) {
/* See if it's an internal command */
handle_internal_command(*argcp, *argv);
/* .. then try the external ones */
execv_dashed_external(*argv);
/* It could be an alias -- this works around the insanity
* of overriding "git log" with "git show" by having
* alias.log = show
*/
if (done_alias || !handle_alias(argcp, argv))
break;
done_alias = 1;
}
return done_alias;
}
Run Code Online (Sandbox Code Playgroud)
所以它不可能.(如果找到命令则handle_internal_command
调用exit
).
您可以通过更改行的顺序并在找到别名时进行handle_alias
调用来解决此问题exit
.
Dav*_*ens 23
如前所述,不可能使用git别名来覆盖git命令.但是,可以使用shell别名覆盖git命令.对于任何POSIXy shell(即不是MS cmd
),编写一个简单的可执行脚本,执行所需的修改行为并设置shell别名.在我的.bashrc
(Linux)和.bash_profile
(Mac)我有
export PATH="~/bin:$PATH"
...
alias git='my-git'
Run Code Online (Sandbox Code Playgroud)
在我的~/bin
文件夹中,我有一个可执行的Perl脚本,它调用my-git
了第一个参数(即git命令)clone
.看起来基本上是这样的:
#!/usr/bin/env perl
use strict;
use warnings;
my $path_to_git = '/usr/local/bin/git';
exit(system($path_to_git, @ARGV))
if @ARGV < 2 or $ARGV[0] ne 'clone';
# Override git-clone here...
Run Code Online (Sandbox Code Playgroud)
我的可配置性更强一些,但你明白了.
ste*_*din 22
我选择用bash函数来解决这个问题.如果我打电话git clone
,它会将呼叫重定向到git cl
,这是我的别名与一些添加的开关.
function git {
if [[ "$1" == "clone" && "$@" != *"--help"* ]]; then
shift 1
command git cl "$@"
else
command git "$@"
fi
}
Run Code Online (Sandbox Code Playgroud)
Cir*_*四事件 16
不仅不可能,还有WONTFIX
目前git不允许别名覆盖内置.我理解这背后的原因,但我想知道它是否过于保守.
它不是.
大多数shell支持使用别名覆盖命令,我不确定为什么git需要比shell更保守.
因为在脚本中使用时,理智的shell不会扩展别名,并且提供了一种方便的方法来甚至从命令行中击败别名.
Run Code Online (Sandbox Code Playgroud)$ alias ls='ls -aF' $ echo ls >script $ chmod +x script
并比较:
Run Code Online (Sandbox Code Playgroud)$ ./script $ ls $ /bin/ls