What exactly does the “u” do? “git push -u origin master” vs “git push origin master”
尽管我尽了最大努力去理解Git,但我显然很难使用它。
来自kernel.org for
-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. in git-config(1)..merge
号
这是来自
branch. .merge Defines, together with
branch. , 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.remote , 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. . 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.remote" from another branch in the local repository, you can point branch. to the desired branch, and use the special setting . (a period) for.merge branch. ..remote
号
我成功地使用Github设置了一个远程存储库,并成功地将我的第一次提交推送到它:
1 | git push -u origin master |
然后,我无意中成功地将第二次提交推送到远程存储库,使用:
1 | git commit -m '[...]' |
号
然而,我错误地认为我必须再次从
1 2 | # note: no -u git push origin master |
那是怎么回事?它似乎一点效果都没有。我"撤销"了
关键是"少争论的git pull"。当您从分支中执行
要了解区别,我们使用一个新的空分支:
1 | $ git checkout -b test |
首先,我们在没有
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. |
号
现在,如果我们加上
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. |
请注意,已设置跟踪信息,以便
更新:奖金提示:
- 正如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 |
如果您忘记了
或者你可以强迫它:
1 2 | git config branch.master.remote origin git config branch.master.merge refs/heads/master |
。
如果您让命令为您执行,它将选择您的错误,比如您键入了一个不存在的分支,或者您没有键入cx1(15),尽管这可能是您想要的:)。
简单来说:
从技术上讲,
这里重要的是,这可以让您在不提供任何其他参数的情况下执行
否则,您必须键入整个命令。
所有必需的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 |
。