博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
“ u”到底是做什么的? “ git push -u原始主机”与“ git push原始主机”
阅读量:2290 次
发布时间:2019-05-09

本文共 6207 字,大约阅读时间需要 20 分钟。

本文翻译自:

I'm apparently terrible at using git, despite my best attempts to understand it. 尽管我尽了最大的努力去理解git,但我显然还是很害怕使用git。

From for git push : 从进行git push

-u -u

--set-upstream --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. 对于每个最新的或成功推送的分支,添加上游(跟踪)引用,该引用由无参数的git-pull(1)和其他命令使用。 For more information, see branch.<name>.merge in git-config(1). 有关更多信息,请参见git-config(1)中的branch.<name>.merge

Here's branch.<name>.merge from git config : 这里的branch.<name>.mergegit config

branch.<name>.merge

Defines, together with branch.<name>.remote , the upstream branch for the given branch. branch.<name>.remote一起定义给定分支的上游分支。 It tells git fetch/git pull which branch to merge and can also affect git push (see push.default). 它告诉git fetch / git pull合并哪个分支,并且还可能影响git push(请参阅push.default)。 When in branch <name> , it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. 在分支<name> ,它告诉git fetch将标记为要合并到FETCH_HEAD的默认refspec。 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" . 该值的处理方式类似于"branch.<name>.remote"的远程部分,并且必须匹配从"branch.<name>.remote"给定的远程获取的ref。 The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. git pull(首先调用git fetch)使用合并信息来查找默认分支进行合并。 Without this option, git pull defaults to merge the first refspec fetched. 如果没有此选项,则git pull默认会合并获取的第一个refspec。 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 . 如果要设置git pull以便它从本地存储库中的另一个分支合并到<name>中,则可以将branch.<name>.merge指向所需的分支,并使用特殊设置。 (a period) for branch.<name>.remote . (句点)代表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 origin from master , I ran: 但是,错误地认为我将不得不再次从master推到origin ,我跑了:

# note: no -ugit 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


#1楼

参考:


#2楼

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 ,请执行最后一条语句。

Or you could force it: 或者,您可以强制执行以下操作:

git config branch.master.remote origingit 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 ; 如果您让命令为您执行此操作,它将选择您的错误,例如您键入了不存在的分支或您没有git remote add though that might be what you want. 尽管那可能就是您想要的。 :) :)


#3楼

All necessary git bash commands to push and pull into Github: 所有需要推送和拉入Github的git bash命令:

git status git pullgit add filefullpathgit commit -m "comments for checkin file" git push origin branch/mastergit 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

#4楼

In more simple terms: 简单来说:

Technically, the -u flag 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 pull without supplying any more arguments. 这里重要的是,这使您无需提供更多参数即可进行git pull For example, once you do a git push -u origin master , you can later call git pull and git will know that you actually meant git pull origin master . 例如,一旦您执行了git push -u origin master ,就可以稍后调用git pull而git将知道您实际上是git pull origin master

Otherwise, you'd have to type in the whole command. 否则,您将必须输入整个命令。


#5楼

The key is "argument-less git-pull". 关键是“无参数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 pull ,未指定源远程或分支时,git会查看branch.<name>.merge设置以了解从何处提取。 git push -u sets this information for the branch you're pushing. 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 pullYou asked me to pull without telling me which branch youwant to merge with, and 'branch.test.merge' inyour configuration file does not tell me, either. Pleasespecify which branch you want to use on the command line andtry again (e.g. 'git pull 
').See git-pull(1) for details.If you often merge with the same branch, you may want touse something like the following in your configuration file: [branch "test"] remote =
merge =
[remote "
"] url =
fetch =
See git-config(1) for details.

Now if we add -u : 现在,如果我们添加-u

$ git push -u origin testBranch test set up to track remote branch test from origin.Everything up-to-date$ git pullAlready up-to-date.

Note that tracking information has been set up so that git pull works as expected without specifying the remote or branch. 请注意,已经设置了跟踪信息,以便git pull可以按预期工作,而无需指定远程或分支。

Update: Bonus tips: 更新:奖金提示:

  • As Mark mentions in a comment, in addition to git pull this setting also affects default behavior of git push . 正如Mark在评论中提到的那样,除了git pull之外,此设置还影响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 . 如果您习惯于使用-u捕获要跟踪的远程分支,则建议将push.default config值设置为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). git push -u <remote> HEAD将当前分支推送到<remote>上同名的分支(并设置跟踪,以便您可以在此之后执行git push )。

转载地址:http://cgcnb.baihongyu.com/

你可能感兴趣的文章