Git alias appending parameters to end

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

I'm using Git Bash v1.8.1, with a few aliases (for testing):

[alias]     ekko = !echo $1 && echo $1     ekko2 = !sh -c 'echo $1 && echo $1'

But when I run them, I see:

> git ekko master master master master

And:

> git ekko2 master (blank line)  (blank line) 

My intended behavior is:

> git ekko master master master

I'm fairly new to aliases - I'm looking for a way to ensure that my arguments are consumed entirely, and not appended to the end of the alias. Some sleuthing indicates this behavior changed somewhere around Git v1.7.x, and I haven't yet determined exactly how to accomplish this:

Git Alias - Multiple Commands and Parameters

回答1:

Your ekko2 alias is really close... What you really want is this:

[alias]     ekko2 = !sh -c 'echo $1 && echo $1' -

Git aliases that execute shell commands do substitute the $n variables, but they also append any arguments you pass them to the end of the command. So in your first example git ekko master is equivalent to echo master && echo master master, which explains its output.

Your second example is closer, but you're passing "master" to the sh command, which is just ignoring the extra argument. By adding - to the end of the alias, you're telling sh that the arguments that follow are intended for the script sh is executing, and not for sh itself.



回答2:

ekko2 is missing a single dash at the end:

[alias]     ekko2 = !sh -c 'echo $1 && echo $1' -

See the Git Wiki on Advanced aliases with arguments:

[alias]     example = !sh -c 'ls $2 $1' -


回答3:

You can add a Bash noop command ":" at the end:

git config --global alias.hello-world '!echo "$1"; :'

Prints:

$ git hello-world hello world hello world


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!