尽管我最好去理解它,但我使用git显然很糟糕.
从kernel.org为git push:
-u
--set上游
对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数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).在分支中<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
Run Code Online (Sandbox Code Playgroud)
然后,我在不知不觉中成功地将我的第二次提交推送到我的远程存储库:
git commit -m '[...]'
Run Code Online (Sandbox Code Playgroud)
然而,错误地以为我将不得不再次推到origin从master,我跑:
# note: no -u
git push origin master
Run Code Online (Sandbox Code Playgroud)
那是做什么的?它似乎没有任何影响.我"撤消"了git push -u origin master吗?
dah*_*byk 303
关键是"无争议的git-pull".当您git pull从分支执行a ,而不指定源远程或分支时,git会查看该branch.<name>.merge设置以了解从哪里提取.git push -u为您正在推送的分支设置此信息.
为了看到差异,让我们使用一个新的空分支:
$ git checkout -b test
Run Code Online (Sandbox Code Playgroud)
首先,我们推动没有-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.
Run Code Online (Sandbox Code Playgroud)
现在,如果我们添加-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.
Run Code Online (Sandbox Code Playgroud)
请注意,已设置跟踪信息,以便在git pull不指定远程或分支的情况下按预期工作.
更新:奖金提示:
git pull设置外还会影响默认行为git push.如果您习惯使用-u捕获要跟踪的远程分支,我建议您将push.default配置值设置为upstream.git push -u <remote> HEAD将当前分支推送到同名的分支<remote>(并设置跟踪,以便您可以在此git push之后执行).sab*_*ton 76
git push -u origin master
Run Code Online (Sandbox Code Playgroud)
是相同的:
git push origin master ; git branch --set-upstream master origin/master
Run Code Online (Sandbox Code Playgroud)
如果忘记了,请做最后的陈述-u!
或者你可以强迫它:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
Run Code Online (Sandbox Code Playgroud)
如果你让命令为你做,它会选择你的错误,就像你输入一个不存在的分支或你没有git remote add,虽然这可能是你想要的:).
Adé*_*gun 37
用更简单的术语来说:
从技术上讲,该-u标志会向您要推送的上游服务器添加跟踪引用.
这里重要的是,这可以让你在git pull不提供任何参数的情况下完成.例如,一旦你做了git push -u origin master,你可以稍后打电话git pull,git就会知道你的意思git pull origin master.
否则,您必须输入整个命令.