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,但是找不到--set-upstream-to

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

这取决于您的 git 版本。 --set-upstream-to 于 2012 年在 1.7.12-1.7.13 时间框架内引入。任何比它更新的版本都应该包含它。这是提交所说的:

commit 6183d826ba62ec94ccfcb8f6e3b8d43e3e338703
Author: Carlos Martín Nieto <cmn@elego.de>
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.

我想说它并没有完全被弃用,但 不推荐使用。我不知道它 was 最近是否被弃用了,但事实上 git-2.7.5 的 git-branch(1) 联机帮助页在没有警告的情况下提到了它,意味着它仍然存在并且将继续存在。你只需要小心。

编辑:抱歉,它 is 在提交 b347d06bf097aca5effd07871adf4d0c8a7c55bd 中被弃用,但这些提交只提到 git-branch,而不是 git-push

这混淆了 git branchgit push

git branch 命令同时具有 --set-upstream--set-upstream-to,前者已弃用,后者已被弃用,原因已在 .[=37 中给出=]

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没有名为[=20=的分支].

但是很快,您 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有关,但不相同。