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

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


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

我顯然很害怕使用git,盡管我盡力去理解它。

From kernel.org for git push:

從kernel.org到git推送:

-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. For more information, see branch.<name>.merge in git-config(1).

對於每一個更新或成功推送的分支,添加上游(跟蹤)引用,使用無參數的git-pull(1)和其他命令。有關更多信息,請參見分支。 。在git-config合並(1)。

Here's branch.<name>.merge from git config:

這是分支。 <名稱> 。從git配置合並:

branch.<name>.merge

分支。 <名稱> .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>.merge to the desired branch, and use the special setting . (a period) for branch.<name>.remote.

定義,加上分支。 <名稱> 。遠程,給定分支的上游分支。它告訴git fetch/git拖動哪個分支進行合並,也可以影響git push(參見push.default)。當在branch 時,它告訴git獲取默認的refspec以在FETCH_HEAD中進行合並。該值的處理方式類似於refspec的遠程部分,並且必須匹配從“branch. .remote”中獲取的遠程引用。git pull(最初調用git fetch)使用合並信息來查找合並的默認分支。如果沒有這個選項,git就會默認將第一個refspec合並到一起。指定多個值以獲得一個章魚合並。如果您希望設置git pull,以便它從本地存儲庫中的另一個分支中合並到 ,您可以點分支。 。合並到所需的分支,並使用特殊設置。(一段)分支。 <名稱> .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:

然后,我在不經意間成功地將我的第二個commit提交到我的遠程存儲庫中:

git commit -m '[...]'

However, incorrectly thinking I would have to push again to origin from master, I ran:

然而,錯誤地認為我將不得不再次從主人那里推到原點,我跑了:

# 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源程序?

3 个解决方案

#1


219  

The key is "argument-less 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 push -u sets this information for the branch you're pushing.

關鍵是“無爭議的git-pull”。當您從一個分支中提取git時,如果沒有指定源遠程或分支,git會查看分支。合並設置,以知道從哪里提取。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 pull works as expected without specifying the remote or branch.

注意,已經設置了跟蹤信息,這樣git就可以按照預期工作,而無需指定遠程或分支。

Update: Bonus tips:

更新:獎金小貼士:

  • As Mark mentions in a comment, in addition to git pull this setting also affects default behavior of 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.
  • 正如Mark在注釋中提到的,除了git之外,這個設置還會影響git推送的默認行為。如果您習慣於使用-u來捕獲要跟蹤的遠程分支,我建議將您的push.default配置值設置為上游。
  • 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 HEAD將當前分支推到 <遠程> 上的同名分支(並設置跟蹤,以便在此之后進行git推送)。

#2


44  

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 origin
git config branch.master.merge refs/heads/master

If you let the command do if 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遠程添加,盡管那可能是你想要的:)。

#3


-2  

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

所有必要的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 

If you want to edit a file then:

如果你想編輯一個文件:

edit filename.* 

To see all branches and their commits:

查看所有分支及其提交:

git show-branch

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/04/18/2e0fd38afa0d96f5c2553b3ad36509c0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com