“ u”到底是做什么的?“ git push -u原始主机”与“ git push原始主机”


333

尽管我尽了最大的努力去理解git,但我显然还是很害怕使用它。

kernel.org获得git push

-u

--set-upstream

对于每个最新的或成功推送的分支,请添加上游(跟踪)引用,该引用由无参数git-pull(1)和其他命令使用。有关更多信息,请参见branch.<name>.mergegit-config(1)。

下面是branch.<name>.merge来自git config

branch.<name>.merge

与一起定义branch.<name>.remote给定分支的上游分支。它告诉git fetch / git pull合并哪个分支,并且还可能影响git push(请参阅push.default)。在branch中时<name>,它告诉git fetch将默认的refspec标记为要合并到FETCH_HEAD中。该值的处理方式类似于refspec的远程部分,并且必须匹配从所给定的从远程获取的ref "branch.<name>.remote"。git pull(首先调用git fetch)使用合并信息来查找默认分支进行合并。如果没有此选项,则git pull默认情况下会合并获取的第一个refspec。指定多个值以获取章鱼合并。如果您希望设置git pull以便它可以<name>从本地存储库中的另一个分支合并到一起,则可以指向branch.<name>.merge到所需的分支,然后使用特殊设置。(一个时期)branch.<name>.remote

我使用github成功设置了一个远程存储库,并使用以下命令成功将其第一次提交:

git push -u origin master

然后,我无意间成功地使用以下命令将第二次提交推送到远程存储库:

git commit -m '[...]'

但是,我错误地认为我将不得不再次推至originfrom master,我跑了:

# note: no -u
git push origin master

那是做什么的?它似乎根本没有任何作用。我“撤消”了git push -u origin master吗?


44
I'm apparently terrible at using git, despite my best attempts to understand it.-我从来没有像现在这样出色地重塑过我。
dgo

Answers:


335

关键是“无参数git-pull”。当您git pull从分支进行操作而未指定源远程或分支时,git会查看branch.<name>.merge设置以了解从何处提取。git push -u为您要推送的分支设置此信息。

要查看区别,让我们使用一个新的空分支:

$ git checkout -b test

首先,我们不使用以下命令-u

$ git push origin test
$ git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.test.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "test"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

现在,如果我们添加-u

$ git push -u origin test
Branch test set up to track remote branch test from origin.
Everything up-to-date
$ git pull
Already up-to-date.

请注意,已经设置了跟踪信息,因此git pull可以在不指定远程或分支的情况下按预期工作。

更新:奖金提示:

  • 正如Mark在评论中提到的,除此git pull设置外,还影响的默认行为git push。如果您习惯于-u捕获要跟踪的远程分支,建议将push.defaultconfig值设置为upstream
  • git push -u <remote> HEAD会将当前分支推到具有相同名称的分支上<remote>(并设置跟踪,以便您可以在此git push之后进行)。

4
git push <remote> <branch>使事情变得明确。如果不进行设置,则git会退回到分支配置设置(使用来设置)git push -u
dahlbyk

2
@dahlbyk我已将您标记为答案,但该评论多少使我感到困惑。在你的答案,你证明了git的后混淆git push origin test(不具备-u)。然后,您证明了git push -u origin test 消除了歧义。有错别字吗,还是我又变得很笨拙?
ClosureCowboy 2011年

1
我认为我们正在互相交谈。:)当我说的git push <remote> <branch>很明确时,我的意思是相对于git push它依赖于分支配置。同样,它git pull <remote> <branch>是明确的,并且git pull依赖于分支配置。一旦你用推-u,都git pushgit pull预期会工作。
dahlbyk

10
@dahlbyk:你的回答是好的,但在你上面您的意见重复一个常见的误解有关git push-除非你设置push.default自己,git push只使用上游分支配置来决定远程推到,而不是远程分支要更新的。
Mark Longair 2011年

1
git的最佳实践git push origin master与另一边的IE相同。git pull origin master..因此,如果假设分支发生变化,那么您可以 git push origin branch_name和另一边的IE一样。git pull origin branch_name
Arpit Vaishnav

87
git push -u origin master

… 是相同的:

git push origin master ; git branch --set-upstream master origin/master

如果忘记了,请执行最后一个语句-u

或者,您可以强制执行以下操作:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

如果您让命令为您执行此操作,它将选择您的错误,例如您键入了不存在的分支或没有键入git remote add;尽管那可能就是您想要的。:)


1
BTW master只是一个例子:)
sabgenton

好的,第二个比特现在已在较新版本中折旧为:git branch master -u origin/masterdetails @ stackoverflow.com/a/2286030/790359
sabgenton 2013年

2
如果您忘记使用该-u选项,则git push -u立即键入即可。
zeekvfu 2014年

1
较新版本的git表示--setup-upstream将不推荐使用: The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
Bill Hoag

--set-upstream已弃用,现在可以使用:git branch --set-upstream-to=origin/master master
Alberto Perez

43

简单来说:

从技术上讲,该-u标志将跟踪参考添加到要推送到的上游服务器。

这里重要的是,这使您git pull无需提供更多参数即可执行操作。例如,一旦执行a git push -u origin master,便可以稍后调用,git pull而git将知道您的实际意思git pull origin master

否则,您将必须输入整个命令。


1
因此,如果我将-u标志设置为orgin master每个下一个拉动都将引用它。如果我想更改git pull行为,我应该运行git push -u origin some_other_branchgit pull现在将参考some_other_branch?谢谢!
Toma Tomov

1
我们还可以使用“ git push”代替“ git push origin master”吗?
cegprakash19年

是的,您可以@cegprakash。但是,你会开始做了git push -u origin master
AdépòjùOlúwáségun

-11

所有必要的git bash命令来推送和拉入Github:

git status 
git pull
git add filefullpath

git commit -m "comments for checkin file" 
git push origin branch/master
git remote -v 
git log -2 

如果要编辑文件,则:

edit filename.* 

要查看所有分支及其提交:

git show-branch

4
我认为您的回答超出了问题的范围。
AdépòjùOlúwáségun
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.