git push -u是什么意思?

Fra*_*ery 317 git

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

从文档中,-u它与变量有关

branch.<name>.merge
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

什么是上游分支?

Raf*_*ler 350

"上游"指的是其他人将从中获取的主要回购,例如您的GitHub回购.-u选项会自动为您设置上游,将您的仓库链接到中央仓库.这样,在未来,Git"知道"你想要推送到哪里以及你想要从哪里开始,所以你可以使用git pullgit push不使用参数.有点下来,这篇文章解释并演示了这个概念.

  • @HaveAGuess可能出于同样的原因,Eclipse也提供了苦难和绝望. (23认同)
  • 我看到你链接到的文章确实指出了这一点,但由于它是一个问题,我认为值得指出用`git push`推动的分支[不受上游分支的影响配置](http://longair.net/blog/2011/02/27/an-asymmetry-between-git-pull-and-git-push/)除非你把`push.default`设置为`tracking`(或者在后来的git版本中的`upstream`). (19认同)

Pet*_*per 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.
Run Code Online (Sandbox Code Playgroud)

  • @JeanPaul - -u选项执行以下操作:对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数git-pull和其他命令使用.因此,在使用-u选项推送本地分支后,此本地分支将自动与远程分支链接,您可以使用git pull而不使用任何参数. (8认同)
  • 但是现在标志`-u`代表什么?GitHub 仍然建议我们在创建新的 repo 时使用这个标志...... (2认同)

小智 6

第一次推送新分支时,请使用: >git push -u origin

之后,您只需键入一个较短的命令: >git push

第一次 -u 选项与您的本地分支创建了一个持久的上游跟踪分支。