“你”到底是做什么的?“git push -u origin master” vs “git push origin master”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5697750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:39:17  来源:igfitidea点击:

What exactly does the "u" do? "git push -u origin master" vs "git push origin master"

git

提问by ClosureCowboy

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

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

From kernel.orgfor git push:

kernel.org获取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>.mergein git-config(1).

-u

--设置上游

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

Here's branch.<name>.mergefrom git config:

这是branch.<name>.merge来自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>.mergeto the desired branch, and use the special setting . (a period) for branch.<name>.remote.

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

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

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

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 originfrom master, I ran:

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

# 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?

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

回答by dahlbyk

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

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

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

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

$ git checkout -b test

First, we push without -u:

首先,我们没有推送-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:

现在,如果我们添加-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 pullworks as expected without specifying the remote or branch.

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

Update:Bonus tips:

更新:奖金提示:

  • As Mark mentions in a comment, in addition to git pullthis setting also affects default behavior of git push. If you get in the habit of using -uto capture the remote branch you intend to track, I recommend setting your push.defaultconfig value to upstream.
  • git push -u <remote> HEADwill push the current branch to a branch of the same name on <remote>(and also set up tracking so you can do git pushafter that).
  • 正如 Mark 在评论中提到的,除了git pull此设置之外,还会影响git push. 如果您习惯使用-u捕获要跟踪的远程分支,我建议将您的push.default配置值设置为upstream.
  • git push -u <remote> HEAD会将当前分支推送到同名分支<remote>(并设置跟踪,以便您可以在此git push之后进行)。

回答by sabgenton

git push -u origin master

… is the same as:

… 是相同的:

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

Do the last statement, if you forget the -u!

做最后一个语句,如果你忘记了-u

Oryou could force it:

或者你可以强制它:

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

If you let the command do it for you, it will pick your mistakes like if you typed a non-existent branch or you didn't git remote add; though that might be what you want. :)

如果你让命令为你做这件事,它会挑选你的错误,比如你输入了一个不存在的分支或者你没有输入git remote add;虽然这可能是你想要的。:)

回答by Adépòjù Olúwáségun

In more simple terms:

更简单地说:

Technically, the -uflag adds a tracking reference to the upstream server you are pushing to.

从技术上讲,该-u标志添加了对您推送到的上游服务器的跟踪引用。

What is important here is that this lets you do a git pullwithout supplying any more arguments. For example, once you do a git push -u origin master, you can later call git pulland git will know that you actually meant git pull origin master.

这里重要的是,这使您可以在git pull不提供更多参数的情况下执行 a 。例如,一旦你做了一个git push -u origin master,你可以稍后调用git pull,git 就会知道你实际上是指git pull origin master.

Otherwise, you'd have to type in the whole command.

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

回答by Kamta Mishra

All necessary git bash commands to push and pull into Github:

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

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 

If you want to edit a file then:

如果要编辑文件,则:

edit filename.* 

To see all branches and their commits:

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

git show-branch