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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45580960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:05:38  来源:igfitidea点击:

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

gitpush

提问by Thor

according to this article, git push --set-upstreamis deprecated and git push --set-upstream-toshould be used instead.

根据这篇文章git push --set-upstream已弃用,git push --set-upstream-to应改为使用。

But when I checked the git pushdocumentation, I can only find --set-upstream, but --set-upstream-tois no where to be found.

但是当我查看git push文档时,我只能找到--set-upstream,而--set-upstream-to无处可寻。

So is --set-upstreamdeprecated? Should I use --set-upstreamor --set-upstream-to?

所以被--set-upstream弃用了?我应该使用--set-upstream还是--set-upstream-to

回答by torek

This mixes up git branchand git push.

这混淆了git branchgit push

The git branchcommand has both --set-upstreamand --set-upstream-to, with the former deprecated in favor of the latter for the reason already given in Nick's answer.

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

The git pushcommand has only -uaka --set-upstream, which takes no argument. It means that if the push succeeds, your local Git should set, as the upstream of a branch reference supplied as your source, the remote-tracking branch corresponding to the destination branch you got the other Git to set, which in many cases, your own Git has just now created in yourrepository because their Git has also just created theirbranch. (Whew!)

git push命令只有-uaka --set-upstream,它不接受任何参数。这意味着如果推送成功,您的本地 Git 应该设置作为源提供的分支引用的上游,远程跟踪分支对应于您让另一个 Git 设置的目标分支,在许多情况下,您的自己的 Git 刚刚在您的存储库中创建,因为他们的 Git 也刚刚创建了他们的分支。(哇!)

That is, suppose you have created a branch newbranch:

也就是说,假设您已经创建了一个分支newbranch

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

and want to set its upstream to origin/newbranch. But if you try, it fails:

并希望将其上游设置为origin/newbranch. 但是如果你尝试,它会失败:

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

because origin/newbranchdoes not exist yet, because the other git at origindoes not have a branch named newbranch.

因为origin/newbranch还不存在,因为另一个 git atorigin没有名为newbranch.

Soon, however, you git pushyour local newbranchto their Git, so that their Git creates newbranchin theirrepository. Now that they dohave a newbranch, yourGit creates your origin/newbranchto remember their newbranch. And nowyou can use git branch --set-upstream-to, but it might be nice if git pushcould do that automatically—and that's the git push --set-upstream, aka -u, option.

但是,很快,git push您就会newbranch成为他们 Git的本地人,以便他们的 Gitnewbranch他们的存储库中创建。现在他们确实有一个newbranch您的Git 会创建您origin/newbranch来记住他们的newbranch. 而现在你可以使用git branch --set-upstream-to,但它可能是好的,如果git push能做到这一点自动-,这就是git push --set-upstream,又名-u,选项。

It's related togit branch --set-upstream-to, but not the same.

与 相关git branch --set-upstream-to,但不相同。

回答by NickD

It depends on your version of git. --set-upstream-towas introduced in 2012 in the 1.7.12-1.7.13 timeframe. Any version more recent than that should include it. This is what the commit said:

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

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.

I would say it's not quite deprecated but it isdiscouraged. I don't know if it wasdeprecated more recently, but the fact the the git-branch(1)manpage for git-2.7.5 mentions it without warning about it, means it's still around and is going to stay around. You just have to be careful.

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

EDIT: sorry, it isdeprecated in commit b347d06bf097aca5effd07871adf4d0c8a7c55bd, but these commits only mention git-branch, not git-push.

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