什么是“u”做? “git push -u origin master” vs“git push origin master” [英] What exactly does the "u" do? "git push -u origin master" vs "git push origin master"

查看:193
本文介绍了什么是“u”做? “git push -u origin master” vs“git push origin master”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我尽力去理解它,但我在使用git时显然非常糟糕。

内核。 org for git push


-u



- set-upstream

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

这里是分支。< name> .merge 来自 git config


分支。< name> .merge



与给定分支的上游分支一起定义分支。< name> .remote 。它告诉git fetch / git pull哪个分支要合并,并且也会影响git push(请参阅push.default)。当在分支< name> 中时,它告诉git读取要在FETCH_HEAD中合并的默认refspec。该值的处理方式与refspec的远程部分相同,并且必须匹配由分支。< name> .remote给出的来自远程的ref。合并信息由git pull(它首先调用git fetch)使用来查找合并的默认分支。如果没有这个选项,git pull默认会合并第一个获取的refspec。指定多个值以获得章鱼合并。如果您希望设置git pull,以便它从本地存储库中的另一个分支中合并到< name> ,您可以指向分支。<名称> .merge 添加到所需的分支,并使用特殊设置。 (一段时间)分支。< name> .remote


我成功地使用github建立了一个远程仓库,并且成功地将我的第一个提交文件推送给它:

  git push -u origin master 

然后,我无意中使用以下方法将第二次提交成功推送到远程存储库:

  git commit -m'[...]'

但是,错误地认为我必须从 master 再次推到 origin ,我跑了:

 #note:no -u 
git push origin master

这是干什么的?它似乎没有任何影响。我是否撤销 git push -u origin master

解决方案

关键是无参数的git-pull。当你从一个分支执行 git pull 时,没有指定源远程或分支,git会查看分支。< name> .merge 设置来知道从哪里拉。 git push -u 为您推送的分支设置此信息。



为了查看差异,让我们使用一个新的空分支:

  $ git checkout -b test 

首先,我们推送时没有 -u

  $ git push origin test 
$ git pull
你问我拉不告诉我你
要合并的分支,'branch.test .merge'在
中,你的配置文件也不会告诉我。请
指定你想在命令行使用哪个分支,
再试一次(例如'git pull< repository>< refspec>')。
有关详细信息,请参阅git-pull(1)。

如果您经常与同一分支合并,您可能希望在您的配置文件中使用类似以下内容的


[branchtest]
remote =<昵称>
merge =< remote-ref>

[远程<昵称>]
url =< url>
fetch =< refspec>

有关详细信息,请参阅git-config(1)。

现在,如果我们添加 -u

  $ git push -u原始测试
分支测试设置为从原点跟踪远程分支测试。
所有最新的
$ git pull
已经是最新的。

请注意,已设置跟踪信息,以便 git pull

更新:奖金提示:


$按预期工作,无需指定远程或分支。 b $ b

  • 随着Mark在评论中提到,除 git pull 外,此设置还会影响 git push 。如果你习惯于使用 -u 捕获你想要跟踪的远程分支,我建议设置你的 push.default config value to upstream

  • git push -u< remote> HEAD 会将当前分支推送到< remote> 上的同名分支(并且还设置了跟踪,因此您可以执行 git push 之后)。


I'm apparently terrible at using git, despite my best attempts to understand it.

From kernel.org for git push:

-u

--set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).

Here's branch.<name>.merge from git 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 and can also affect git push (see push.default). When in branch <name>, it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. The value is handled like the remote part of a refspec, and must match a ref which is fetched from the remote given by "branch.<name>.remote". The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. Without this option, git pull defaults to merge the first refspec fetched. Specify multiple values to get an octopus merge. If you wish to setup git pull so that it merges into <name> from another branch in the local repository, you can point branch.<name>.merge to the desired branch, and use the special setting . (a period) for branch.<name>.remote.

I successfully set up a remote repository with github, and I successfully pushed my first commit to it with:

git push -u origin master

Then, I unwittingly successfully pushed my second commit to my remote repository using:

git commit -m '[...]'

However, incorrectly thinking I would have to push again to origin from master, I ran:

# note: no -u
git push origin master

What did that do? It didn't seem to have any effect at all. Did I "undo" git push -u origin master?

解决方案

The key is "argument-less git-pull". When you do a git pull from a branch, without specifying a source remote or branch, git looks at the branch.<name>.merge setting to know where to pull from. git push -u sets this information for the branch you're pushing.

To see the difference, let's use a new empty branch:

$ git checkout -b test

First, we push without -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.

Now if we add -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.

Note that tracking information has been set up so that git pull works as expected without specifying the remote or branch.

Update: Bonus tips:

  • As Mark mentions in a comment, in addition to git pull this setting also affects default behavior of git push. If you get in the habit of using -u to capture the remote branch you intend to track, I recommend setting your push.default config value to upstream.
  • git push -u <remote> HEAD will push the current branch to a branch of the same name on <remote> (and also set up tracking so you can do git push after that).

这篇关于什么是“u”做? “git push -u origin master” vs“git push origin master”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆