git push -u是什么意思?


323

我有两个不同版本的git。在1.6.2版本中,git push没有该-u选项。它仅出现在1.7.x版本中。

在文档中,-u与变量相关

branch.<name>.merge

在中git config。此变量描述如下:

Defines, together with branch.<name>.remote, the upstream branch 
for the given branch. It tells git fetch/git pull which branch to merge.

什么是上游分支?


Answers:


360

“上游”是指其他人将从中获取的主要存储库,例如您的GitHub存储库。-u选项自动为您设置该上游,从而将您的存储库链接到中央存储库。这样,将来Git会“知道”您要推送到的位置以及您要从哪里提取的信息,因此您可以使用git pullgit push不使用参数。稍有下降,本文解释并演示了此概念。


19
我看到您链接到的文章确实指出了这一点,但是由于这有点麻烦,我认为值得指出的是,除非您进行了设置,否则要推送到的分支git push 不受上游分支配置的影响push.defaulttracking(或upstream在更高版本的git中)。
Mark Longair 2011年

我不明白为什么日食会同时提供两者?
HaveAGuess 2012年

23
@HaveAGuess也许出于同样的原因,Eclipse也提供了痛苦和绝望。
twiz 2015年

9

这不再是最新的!

Push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

1
但是标志-u现在代表什么?它仍然建议GitHub上使用此标志,当我们创建一个新的回购...
让·保罗·

8
@JeanPaul--u选项执行以下操作:对于每个最新的或成功推送的分支,添加上游(跟踪)引用,该引用由无参数git-pull和其他命令使用。因此,在使用-u选项推送本地分支之后,该本地分支将自动与远程分支链接,并且您可以使用不带任何参数的git pull。
彼得·派珀
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.