git push --set-upstream vs --set-upstream-to

git push --set-upstream vs --set-upstream-to

问题描述:

根据此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

这混合了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

这取决于你的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