为什么我需要一直做`--set-upstream`? [英] Why do I need to do `--set-upstream` all the time?
问题描述
我在 Git 中创建了一个新分支:
I create a new branch in Git:
git branch my_branch
推动它:
git push origin my_branch
现在假设有人对服务器进行了一些更改,我想从 origin/my_branch
中提取.我愿意:
Now say someone made some changes on the server and I want to pull from origin/my_branch
. I do:
git pull
但我明白了:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch "my_branch"]
remote = <nickname>
merge = <remote-ref>
[remote "<nickname>"]
url = <url>
fetch = <refspec>
See git-config(1) for details.
我了解到我可以使用它:
I learned that I can make it work with:
git branch --set-upstream my_branch origin/my_branch
但是为什么我需要为我创建的每个分支都这样做?如果我将 my_branch
推入 origin/my_branch
,那么我想将 origin/my_branch
拉入 my_branch 不是很明显吗?代码>?如何将其设为默认行为?
But why do I need to do this for every branch I create? Isn't it obvious that if I push my_branch
into origin/my_branch
, then I would want to pull origin/my_branch
into my_branch
? How can I make this the default behavior?
推荐答案
一个快捷方式,不依赖于记住git branch --set-upstream
1的语法sup> 是这样做的:
A shortcut, which doesn't depend on remembering the syntax for git branch --set-upstream
1 is to do:
git push -u origin my_branch
... 第一次推送该分支.或者,从同名分支推送到当前分支(对于别名很方便):
... the first time that you push that branch. Or, to push to the current branch from a branch of the same name (handy for an alias):
git push -u origin HEAD
你只需要使用一次 -u
,就可以像 git branch 一样设置你的分支和
确实如此.origin
的分支之间的关联--set-upstream
You only need to use -u
once, and that sets up the association between your branch and the one at origin
in the same way as git branch --set-upstream
does.
就我个人而言,我认为必须明确地在您的分支和远程分支之间建立这种关联是一件好事.很遗憾,规则是 git push
和 git pull
不同.
Personally, I think it's a good thing to have to set up that association between your branch and one on the remote explicitly. It's just a shame that the rules are different for git push
and git pull
.
1 这听起来可能很傻,但我经常忘记指定当前分支,假设这是默认的 - 它不是,结果最令人困惑.
1 It may sound silly, but I very frequently forget to specify the current branch, assuming that's the default - it's not, and the results are most confusing.
2012 年 10 月 11 日更新:显然,我不是唯一一个发现容易出错的人!感谢 VonC 指出 git 1.8.0 引入了更明显的 git branch --set-upstream-to
,如果你在分支 my_branch
上,可以如下使用:
Update 2012-10-11: Apparently I'm not the only person who found it easy to get wrong! Thanks to VonC for pointing out that git 1.8.0 introduces the more obvious git branch --set-upstream-to
, which can be used as follows, if you're on the branch my_branch
:
git branch --set-upstream-to origin/my_branch
... 或使用短选项:
... or with the short option:
git branch -u origin/my_branch
此更改及其原因在 git 1.8.0 的发行说明,候选发行版 1:
很想说 git branch --set-upstream origin/master
,但这告诉 Git 安排本地分支 origin/master
与当前签出分支,这极不可能是用户的意思.该选项已弃用;改用新的 --set-upstream-to
(带有简短的 -u
)选项.
It was tempting to say
git branch --set-upstream origin/master
, but that tells Git to arrange the local branchorigin/master
to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new--set-upstream-to
(with a short-and-sweet-u
) option instead.
这篇关于为什么我需要一直做`--set-upstream`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!