git alias是否可以在我必须在两个地方重复相同文本的情况下使用参数?

git alias是否可以在我必须在两个地方重复相同文本的情况下使用参数?,git,Git,如果不使用shell,我可以在git别名上使用参数吗 例如,我的主要远程分支始终称为“上游”,有时git会丢失与我的本地分支的引用,并说: There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch>

如果不使用shell,我可以在git别名上使用参数吗

例如,我的主要远程分支始终称为“上游”,有时git会丢失与我的本地分支的引用,并说:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=<remote>/<branch> master
所以我可以只键入
git reup master


(或者更好,将git配置为始终假定remote为“上游”?)

是的,您可以使用参数创建
git
别名,您只需要简单的技巧,将#放在行尾,否则git将再次追加参数。 因此,在您的情况下,这应该是可行的:

[alias]
    reup = "!git branch --set-upstream-to=upstream/$1 $1 #"
但事实上,这不是你真正的问题,更像是:如何将origin重命名为上游

查看一下git remote,这将有助于:

   rename
       Rename the remote named <old> to <new>. All remote-tracking branches and configuration settings for the remote are updated.
如果您需要它,那么您根本不需要别名


(否则“origin”名称在这里是硬编码的:因此您需要更改它并重新编译git)

而不使用shell?没有。但是对于shell,当然:

[alias]
brup = "!f() { git branch --set-upstream-to=${2:-origin}/$1 $1; }; f"

现在您可以运行
git brup dev
将上游设置为
origin/dev
git brup dev upstream
将上游设置为
upstream/dev

我确实有上游和原点。origin是我的用户forks。上游团队回购。我确实需要与上游进行更多的同步,但我不想在默认情况下推送到上游(如果重命名它,会发生这种情况……但这是一个很好的建议),您应该在问题中添加这些约束。如果您同时需要
origin
upstream
,那么除了像您一样使用某种别名之外,我看不到设置默认值的方法。问题以“不使用shell…”开头,而答案是带有
的别名-即外壳!;-)
git remote rename origin upstream
[alias]
brup = "!f() { git branch --set-upstream-to=${2:-origin}/$1 $1; }; f"