要在 git 别名中运行命令,尤其是向这些命令传递参数,您可能必须创建一个临时函数,然后立即调用该函数:
$ vim ~/.gitconfig
...
[alias]
# compare:
foo = "! echo begin arg=$1/$2/end"
foo2 = "!f() { echo "begin arg=$1/$2/end"; }; f"
在这个例子中,这个函数可能是你所需要的(并且对于你可以在单个“语句”中做什么也更加灵活);而且您可能会说,对于这两个选项,git 命令的剩余参数只是作为参数传递给别名,无论它是“echo”还是“f”;调用该函数只会消耗 args,而忽略未明确使用的内容:
$ git foo a b c
begin arg=a/b/end a b c
$ git foo2 a b c
begin arg=a/b/end
另一个示例(根据匹配模式列出所有别名)(注意:您可以在整个 .gitconfig 中继续重用相同的函数名称“f()”):
[alias]
alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f"
第一个返回“foo$”的别名,第二个返回“foo.*”的别名:
$ git alias foo
alias.foo ! echo begin arg=$1/$2/end
$ git alias 'foo.*'
alias.foo ! echo begin arg=$1/$2/end
alias.foo2 !f() { echo begin arg=$1/$2/end; }; f
(注意:实际结果可能因 shell 而异;我在 Linux、Unix 和 Cygwin (Windows) 上将其与 bash 一起使用。)