2017-08-09 98 views
3

根據此articlegit push --set-upstream已被棄用,應該使用git push --set-upstream-to來代替。git push --set-upstream vs --set-upstream-to

但是當我檢查git push文檔時,我只能找到--set-upstream,但--set-upstream-to是沒有在哪裏可以找到。

那麼--set-upstream已棄用?我應該使用--set-upstream還是--set-upstream-to

回答

7

這混合了git branchgit push

git branch命令有兩種--set-upstream--set-upstream-to,前者有利於後者在Nick's answer已經給出的理由的棄用。

git push命令只有-u又名--set-upstream,它沒有參數。這意味着如果推送成功,你的本地Git應該設置一個作爲源的分支引用的上游,與目標分支相對應的遠程跟蹤分支,你可以設置另一個Git,在很多情況下,你的自己的Git剛剛在中創建了你的存儲庫,因爲他們的Git也剛剛創建了他們的分支。 (唷!)

也就是說,假設你已經創建了一個分支newbranch

$ git checkout -b newbranch 
... work, commit, etc 

,並希望其上游設置爲origin/newbranch。但是,如果你嘗試,它失敗:

$ git branch --set-upstream-to=origin/newbranch 
error: the requested upstream branch 'origin/newbranch' does not exist 

因爲origin/newbranch還不存在,因爲origin其他Git並不有一個名爲newbranch分支。

很快,但是,你git push當地newbranch自己的Git,讓自己的Git創建newbranch他們庫。現在他們有一個newbranch,你的 Git創建你的origin/newbranch記住他們的newbranch。而現在你可以使用git branch --set-upstream-to,但它可能很好,如果git push可以自動做到這一點 - 這就是git push --set-upstream,又名-u,選項。

這是有關git branch --set-upstream-to,但不一樣。

+0

啊,這清除了我的困惑。非常感謝您的幫助! – Thor

+0

我也是!謝謝。 – Nick

1

這取決於你的git版本。 --set-upstream-to於2012年7月1日至7月7日推出。任何比這更新的版本都應該包括它。這就是承諾說:

commit 6183d826ba62ec94ccfcb8f6e3b8d43e3e338703 
Author: Carlos Martín Nieto <[email protected]> 
Date: Mon Aug 20 15:47:38 2012 +0200 

branch: introduce --set-upstream-to 

The existing --set-uptream option can cause confusion, as it uses the 
usual branch convention of assuming a starting point of HEAD if none 
is specified, causing 

    git branch --set-upstream origin/master 

to create a new local branch 'origin/master' that tracks the current 
branch. As --set-upstream already exists, we can't simply change its 
behaviour. To work around this, introduce --set-upstream-to which 
accepts a compulsory argument indicating what the new upstream branch 
should be and one optinal argument indicating which branch to change, 
defaulting to HEAD. 

The new options allows us to type 

    git branch --set-upstream-to origin/master 

to set the current branch's upstream to be origin's master. 

我會說這是不是很過時,但它氣餒。我不知道最近是否被棄用了,但事實上git-2.7.5的聯機幫助頁提到了它,但沒有提出任何警告,這意味着它仍然存在,並且會留在身邊。你只需要小心。

編輯:對不起,這是不贊成在提交b347d06bf097aca5effd07871adf4d0c8a7c55bd,但這些提交只提git-branch,不git-push

+0

嗨,尼克,感謝您的答案!如果我可能會問,你能否告訴我當你尋找你在答案中提到的特定提交時,你的搜索策略是什麼?我的意思是git的git存儲庫包含數千個提交,你怎麼能找到你想要的? – Thor

+1

我做了'git log --grep = set-upstream-to',看了看最後一個(但我錯過了要求編輯的最後一個:-)) – Nick

+0

啊,是的,我忘了關於正則表達式。謝謝你讓我知道。真的很感激:) – Thor