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

尽pipe我尽力去理解它,但是我显然在使用git时很糟糕。

从kernel.org for git push

-u

–set上游

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

这里是branch.<name>.merge from git config

branch.<name>.merge

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

我成功地用githubbuild立了一个远程仓库,并且成功地将我的第一个提交交给了它:

 git push -u origin master 

然后,我不知不觉地成功地把我的第二个提交到我的远程仓库使用:

 git commit -m '[...]' 

然而,错误的想法,我将不得不再次推动来自master ,我跑了:

 # note: no -u git push origin master 

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

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

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

 $ 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 (eg '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. 

请注意,跟踪信息已经build立,以便git pull按预期工作,无需指定远程或分支。

更新:奖金提示:

  • 正如Mark在评论中提到的,除了git pull之外,这个设置还会影响git push默认行为。 如果您习惯使用-u来捕获您想要跟踪的远程分支,我build议将您的push.defaultconfiguration值设置为upstream
  • git push -u <remote> HEAD会将当前分支推送到<remote>上的同名分支(并设置跟踪,以便在此之后执行git push )。
  git push -u origin master 

是相同的:

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

如果您忘记了-u做最后的陈述!

或者你可以强制它:

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

如果你让命令执行,如果你喜欢,如果你键入一个不存在的分支,或者你没有git remote add ,但是这可能是你想要的:)。

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

 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 

如果你想编辑一个文件,那么:

 edit filename.* 

查看所有分支及其提交:

 git show-branch