“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"

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

问题描述

尽管我尽了最大努力去理解它,但我显然不擅长使用 git.

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

来自 kernel.org 用于 git push:

-u

--设置上游

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

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).

这是来自 git configbranch..merge:

branch..merge

branch..remote 一起定义给定分支的上游分支.它告诉 git fetch/git pull 要合并哪个分支,也可以影响 git push(参见 push.default).当在分支 中时,它告诉 git fetch 默认的 refspec 被标记为在 FETCH_HEAD 中合并.该值的处理类似于 refspec 的远程部分,并且必须匹配从 "branch..remote" 给出的远程获取的 ref.合并信息被 git pull(它首先调用 git fetch)用来查找默认分支进行合并.如果没有这个选项,git pull 默认合并第一个获取的 refspec.指定多个值以获得章鱼合并.如果您希望设置 git pull 以便它从本地存储库中的另一个分支合并到 中,您可以将 branch..merge 指向所需的分支,并使用特殊设置.(句号)用于 branch..remote.

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.

我成功地使用 github 设置了一个远程存储库,并且我成功地将我的第一个提交推送到了它:

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 '[...]'

然而,我错误地认为我必须再次从 master 推送到 origin,我跑了:

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

# note: no -u
git push origin master

这是做什么的?它似乎根本没有任何影响.我是否撤消"了git push -u origin master?

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

推荐答案

关键是无参数的 git-pull".当您从分支执行 git pull 时,没有指定源远程或分支,git 会查看 branch.<name>.merge 设置以了解从何处拉取从.git push -u 为您正在推送的分支设置此信息.

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

首先,我们推送没有-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 在不指定远程或分支的情况下按预期工作.

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

更新:额外提示:

  • 正如 Mark 在评论中提到的,除了 git pull 这个设置还会影响 git push 的默认行为.如果您习惯使用 -u 来捕获要跟踪的远程分支,我建议将您的 push.default 配置值设置为 upstream.
  • git push -u <远程>HEAD 会将当前分支推送到 上的同名分支(并设置跟踪,以便您可以在之后执行 git push).
  • 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天全站免登陆