2011-04-06 187 views
280

我有两个不同版本的git。 在1.6.2版本中,git push没有-u选项。它只出现在1.7.x版本中。git push -u是什么意思?

从文档中,-ugit config相关的变量

branch.<name>.merge 

。这个变量描述如下:

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

什么是上游分支?

+1

参见http://stackoverflow.com/questions/2739376/definition-of-downstream-and-upstream/2749166#2749166 – VonC 2011-04-06 04:49:22

回答

299

“上游”指的是其他人将从其中撤回的主要回购。你的GitHub回购。 -u选项自动为您设置上游,将您的repo链接到中央服务器。这样,未来,Git会“知道”你想推到哪里以及你想从哪里拉出来,所以你可以不带参数地使用git pullgit push。一点点下来,this article解释和演示这个概念。

+18

我看到你链接的文章也指出这一点,但由于这是一个棘手的问题,我认为值得指出的是用'git push'推送到的分支[不受上游分支配置的影响](http://longair.net/blog/2011/02/27/an-asymmetry-between-git-pull-and-git-push /),除非你有'push.default'设置为'tracking'(或在git的后续版本中为'upstream')。 – 2011-04-22 11:59:14

+0

我不明白为什么eclipse egit提供两个呢? – HaveAGuess 2012-12-05 22:22:10

+14

@HaveAGuess可能出于同样的原因,Eclipse也提供苦难和绝望。 – twiz 2015-04-17 02:34:33

6

这不再是最新的!

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'现在代表什么?当我们创建新的repo时,GitHub仍建议使用此标志... – 2017-10-17 09:24:18

+3

@JeanPaul - -u选项执行以下操作:对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数的git-pull和其他命令使用。 因此,使用-u选项推送本地分支后,此本地分支将自动链接到远程分支,并且您可以使用没有任何参数的git pull。 – 2017-10-18 07:43:26