“你”究竟做了什么?


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

尽管我尽了最大努力去理解Git,但我显然很难使用它。

来自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..merge in git-config(1).

这是来自git configbranch..merge

branch..merge

Defines, together with branch..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 , 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..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 from another branch in the local repository, you can point branch..merge to the desired branch, and use the special setting . (a period) for branch..remote.

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

1
git push -u origin master

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

1
git commit -m '[...]'

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

1
2
# note: no -u
git push origin master

那是怎么回事?它似乎一点效果都没有。我"撤销"了git push -u origin master吗?


关键是"少争论的git pull"。当您从分支中执行git pull操作时,如果不指定源远程或分支,Git会查看branch..merge设置以了解从何处提取。git push -u为您要推的分支设置此信息。

要了解区别,我们使用一个新的空分支:

1
$ git checkout -b test

首先,我们在没有-u的情况下推进:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ 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

1
2
3
4
5
$ 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.default配置值设置为upstream
  • git push -u HEAD将当前分支推到上的同名分支(并设置跟踪,以便在该分支后执行git push)。


1
git push -u origin master

与以下内容相同:

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

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

或者你可以强迫它:

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

如果您让命令为您执行,它将选择您的错误,比如您键入了一个不存在的分支,或者您没有键入cx1(15),尽管这可能是您想要的:)。


简单来说:

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

这里重要的是,这可以让您在不提供任何其他参数的情况下执行git pull。例如,一旦你做了一个git push -u origin master,你可以稍后打电话给git pull,Git就会知道你实际上是指git pull origin master

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


所有必需的git bash命令,以推动和拉入github:

1
2
3
4
5
6
7
8
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

如果要编辑文件,请执行以下操作:

1
edit filename.*

查看所有分支及其承诺:

1
git show-branch