git push --set-upstream 与 --set-upstream-to

问题描述 投票:0回答:2

根据这篇文章

git push --set-upstream
已被弃用,应使用
git push --set-upstream-to
来代替。

但是当我查看git push文档时,我只能找到

--set-upstream
,但是
--set-upstream-to
却找不到在哪里。

那么

--set-upstream
是否已被弃用?我应该使用
--set-upstream
还是
--set-upstream-to

git push
2个回答
37
投票

这混淆了

git branch
git push

git branch
命令同时具有
--set-upstream
--set-upstream-to
,前者已被弃用,而支持后者,原因已在Nick的回答中给出。

git push
命令只有
-u
又名
--set-upstream
,它不带任何参数。这意味着,如果推送成功,您的本地 Git 应该设置作为源分支引用的上游,与您让其他 Git 设置的目标分支相对应的远程跟踪分支,在许多情况下,您的自己的 Git 刚刚在your存储库中创建,因为他们的 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。现在他们do有一个
newbranch
yourGit创建你的
origin/newbranch
来记住他们的
newbranch
现在您可以使用
git branch --set-upstream-to
,但如果
git push
可以自动执行此操作可能会很好 - 这就是
git push --set-upstream
,又名
-u
,选项。

它与 git branch --set-upstream-to

 
相关,但不一样。


7
投票

这取决于你的git版本。

--set-upstream-to
于 2012 年在 2012 年 7 月 1 日至 2013 年 7 月 1 日期间引入。任何比该版本更新的版本都应该包含它。这就是提交所说的:

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 的 git-branch(1) 手册页在没有警告的情况下提到了它,这意味着它仍然存在并将继续存在。你一定要小心。

编辑:抱歉,它

is

在提交b347d06bf097aca5effd07871adf4d0c8a7c55bd中已弃用,但这些提交仅提到git-branch,而不是

git-push
编辑:@redestructa 在评论中提到 

--set-upstream

完全消失了:

meanwhile: fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead

© www.soinside.com 2019 - 2024. All rights reserved.