如何配置git push以在没有-u的情况下自动设置上游?

Joh*_*ohn 67 git

我想git push origin在第一次推送本地创建的分支时自动设置上游引用.

我知道git push -u,但我不想考虑我以前是否使用-u过或以其他方式设置上游参考.换句话说,我希望git push自动具有对git push -u没有上游的分支的任何推动的影响.

这可能吗?如果它需要别名或实用程序脚本,那很好.

And*_*nzo 26

您可以git config使用配置它git config --global push.default current.

文档:https://git-scm.com/docs/git-config#git-config-pushdefault

  • 我已经设置好了-这就是我可以说`git push origin`而没有refspec或上游的方式。但这对自动设置上游没有帮助。 (4认同)
  • 是的,正如@John 所说,重要的是要记住这 ** 不会** 使本地分支跟踪远程分支;它只是创建与本地分支同名的远程分支。 (2认同)

mec*_*ish 25

由于我不认为使用git配置是可行的,所以你可以在bash中做到这一点:

[[ $(git config "branch.$(git rev-parse --abbrev-ref HEAD).merge") = '' ]] && git push -u || git push
Run Code Online (Sandbox Code Playgroud)

如果当前分支具有远程跟踪分支,则调用git push它,否则调用 git push -u

  • 你现在可以做`git config --global push.default current`. (20认同)
  • @AndreaBergonzo这是我唯一的好答案,你可以补充一下吗? (2认同)
  • @AndreaBergonzo,@pdem-注意`push.default = current`只会在远程存储库中创建一个与本地分支同名的分支,但没有**未设置本地分支来跟踪远程分支。我不确定为什么会这样,但是值得牢记。 (2认同)

Von*_*onC 23

注意:新的默认推送策略" simple"依赖于具有上游分支的分支这一事实意味着:
设置上游分支被视为自愿步骤,而不是隐藏的自动步骤

当" git push [$there]"没有说要推送什么时,到目前为止我们已经使用了传统的"匹配"语义(只要那里已经有相同名称的分支,所有分支都被发送到远程).

我们将使用" simple"语义将当前分支推送到具有相同名称的分支,仅当当前分支设置为与该远程分支集成时.
有一个用户首选项配置变量" push.default"来改变它.


因此,根据turbfish答案,您可以使用右双引号(")escaped(\")来定义别名:

git config alias.pu "![[ $(git config \"branch.$(git rev-parse --abbrev-ref HEAD).merge\") = '' ]] && git push -u || git push"

git pu origin
Run Code Online (Sandbox Code Playgroud)

Sc0ttyD 在评论中提出以下别名:

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'
Run Code Online (Sandbox Code Playgroud)

在多行中:

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && 
           git push -u origin $(git symbolic-ref --short HEAD) || 
           git push'
Run Code Online (Sandbox Code Playgroud)

  • 感谢您展示如何设置别名。不过,我不清楚与您答案的第一部分的联系或相关性。 (2认同)
  • @John我的观点是:你会绕过一个应该是故意的一步.您可以设置该别名,但我想向其他读者说明为什么这个显式的`-u`选项存在,以及为什么没有自动生成所述选项的配置(因此别名). (2认同)
  • 我的.zshrc中有一个zsh别名列表.我修改了这个答案来创建以下zsh别名:`alias gpu ='[[-z $(git config"branch.$(git symbolic-ref --short HEAD).merge")]] && git push -u origin $(git symbolic-ref --short HEAD)|| git push'` (2认同)
  • @ Sc0ttyD有意思,谢谢.我已将您的评论包含在答案中以获得更多可见性. (2认同)

Fre*_*xuz 15

我遇到了同样的问题.我发现了这个别名(.gitconfig)

[alias] track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"

用法:git track每个新分支一次(目前已检出).然后按正常推:)


Mar*_*ark 13

@VonC和@Frexuz的答案很有帮助,但他们的解决方案都给我带来了错误.使用他们的答案,我拼凑了一些对我有用的东西:

    [alias]
    pu = ![[ $(git config "branch.$(git symbolic-ref --short HEAD).merge") = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push
Run Code Online (Sandbox Code Playgroud)

这导致执行git push -u origin $BRANCHNAME或者git push,取决于是否branch.$BRANCHNAME.merge定义了其上游(属性).

在命令行中输入此别名将需要转义码,因此最简单的方法是使用编辑器插入正确的文件($HOME/.gitconfig(全局),.git/config(本地)或/etc/gitconfig(系统))

  • 这是最直接,最完整的答案.要打开默认编辑器而不寻找文件,可以使用`git config --global -e` (3认同)

小智 7

我通过使用这个简单的 Bash 脚本解决了这个问题。它不适用于现有分支,但如果您使用此功能创建所有分支,您将始终自动设置上游分支。

function con { git checkout -b $1 && git push --set-upstream origin $1; }
Run Code Online (Sandbox Code Playgroud)

$1 代表您之后传递的第一个参数,con因此就像这样做:

git checkout -b my-new-branch && git push -u my-new-branch
Run Code Online (Sandbox Code Playgroud)

...通过这样做:

con my-new-branch
Run Code Online (Sandbox Code Playgroud)


Bor*_*ein 5

简答

如果您确实喜欢明确并-u在必要时使用该选项,但只是不想输入整个内容:

git push -u origin foo
Run Code Online (Sandbox Code Playgroud)

然后您可以使用以下别名:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)
Run Code Online (Sandbox Code Playgroud)

只需输入:

git push-u
Run Code Online (Sandbox Code Playgroud)

长答案

通常,需要-u(shorthand for --set-upstream) 是当我们刚刚创建了一个新的本地分支并提交时,我们希望将其推送到上游。远程仓库还没有新的分支,所以我们需要在推送提交之前告诉 git 创建和跟踪远程分支。这仅在第一次推送分支时需要。下面是一个典型的场景:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push -u origin foo      # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我确实喜欢git push -u在创建远程分支时需要明确说明:这是一项非常重要的操作,向全世界共享一个全新的分支。

但是,我讨厌我们必须明确地编写git push -u origin foo. 不仅打字很痛苦,而且更重要的是,它很容易出错!输入分支名称很容易出错,新的远程分支不会和你本地分支同名!在大多数情况下,实际上,您希望上游存储库为origin,并且上游分支与您的本地分支具有相同的名称。

因此,我在 my 中使用了以下别名.gitconfig,这是Mark 提供的优秀答案的一个子集:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)
Run Code Online (Sandbox Code Playgroud)

现在,我们可以执行以下操作,这仍然是明确的,但不太容易出错:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push-u                  # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit
Run Code Online (Sandbox Code Playgroud)


gaz*_*rgo 5

如果您只想通过尽可能少的按键来使用内置的 git 功能,只需输入:

$ git push -u o tab H tab

自动完成会给你 $ git push -u origin HEAD

要在 OSX 上启用自动编译,请设置一个包含此内容的~/.git-completition.bash文件,并将以下行添加到您的文件中,然后重新启动终端:~/.bash_profile

# git branch autocomplete
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Run Code Online (Sandbox Code Playgroud)

它也会影响内置终端,例如 vscode 等。


kor*_*ona 5

对此唯一完全诚实的答案是“你不能”。

我已阅读此问题中的所有回复以及提出相同问题的其他问题。

发布的每个答案仍然需要您在第一次推送到新分支时传递特殊参数。


小智 3

今天我遇到了(一个新的?)选项“push.autoSetupRemote”。git 配置帮助说:

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch; this option takes effect with push.default options simple,
       upstream, and current. It is useful if by default you want new branches to be pushed to the default remote (like the behavior of push.default=current) and you also want
       the upstream tracking to be set. Workflows most likely to benefit from this option are simple central workflows where all branches are expected to have the same name on
       the remote.
Run Code Online (Sandbox Code Playgroud)