git alias:多个命令,可变参数 [英] git alias: multiple commands, variadic parameters
问题描述
我经常发现自己输入以下内容:
I often find myself typing this:
git push remote1 branch1 branch2 tag1 tag2 tag3..
git push remote2 branch1 branch2 tag1 tag2 tag3..
我更喜欢在其中可以键入的别名:
I would prefer an alias where I can type this instead:
git pushall branch1 branch2 tag1 tag2 tag3 ..
注意:我知道我可以使用多个URL创建一个新的远程"all".我们不要在这里讨论这个问题,而要关注别名!
我可以对远程名称进行硬编码,因为我有许多项目具有相同的多个远程名称(通常是"drupal"和"github").
I am ok to hardcode the remote names, because I have a number of projects with the same multiple remote names (usually "drupal" and "github").
我已经想出了一个非可变版本:
I already figured out a non-variadic version:
[alias]
pushall = "!git push github $1; git push drupal $1; #"
这里有两个窍门
- 使用双引号防止';'在
.ini
文件中具有特殊含义 -
#
忽略其余部分.
- using double quotes to prevent ';' from having a special meaning in
.ini
files #
to ignore the rest of the line.
但这一次只能推送一个分支(或标签).所以我必须输入:
But this only pushes one branch (or tag) at a time. So I would have to type this:
git pushall branch1
git pushall branch2
git pushall tag1
git pushall tag2
git pushall tag3
...
我希望可以在其中键入以下内容的别名:
I would prefer an alias where I can type this:
git pushall branch1 branch2 tag1 tag2 tag3 ..
为什么没有具有多个推送URL的新远程全部"?
如上所述,让我们专注于别名,以便读者找到他们想要的东西.
Why not a new remote "all" with multiple push urls?
As said, let's focus on the aliases, so that readers find what they are looking for.
无论如何,这就是为什么我不创建远程全部"的原因:
Anyway, here is why I am not creating a remote "all":
- 我每个项目都必须这样做一次,并且不能在全球范围内这样做.就我而言,使用全局别名对远程名称进行硬编码实际上是可以的!
- Afaik,我将使用"all/branch1"之类的引用代替"remote1/branch1"和"remote2/branch1"等污染我的历史.
讨论此问题的正确位置将在这里,从多个远程位置拉/推
The correct place to discuss this would be here, pull/push from multiple remote locations
以下内容是相关的,但它们不涉及可变参数:
The following are related, but they do not address variadic parameters:
- Git Alias - Multiple Commands and Parameters
- Syntax for Git aliases with multiple commands
- Git alias with positional parameters
以下内容可能会有所帮助,但它可以处理纯shell脚本,而不是git别名:
The following might be helpful, but it addresses pure shell script, not specifically git aliases:
推荐答案
将任意脚本打包为git别名的惯用法是将其放在shell函数中:
The idiom to package an arbitrary script into a git alias is to put it inside a shell function:
pushall = "! f() { git push github \"$@\"; git push drupal \"$@\"; }; f"
我想指出,$@
的正确用法是将其放在双引号内:"$@"
.
I want to point out that the correct use of $@
is to place it inside double-quotes: "$@"
.
这篇关于git alias:多个命令,可变参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!