小编典典

为什么我需要一直做`--set-upstream`?

git

我在 Git 中创建了一个新分支:

git branch my_branch

推它:

git push origin my_branch

现在说有人在服务器上做了一些更改,我想从origin/my_branch. 我做:

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.

我了解到我可以使用它:

git branch --set-upstream my_branch origin/my_branch

但是为什么我需要为我创建的每个分支都这样做呢?my_branch如果我推入origin/my_branch,那么我想拉origin/my_branch入,这不是很明显my_branch吗?我怎样才能使它成为默认行为?


阅读 209

收藏
2022-02-18

共2个答案

小编典典

不依赖于记住git branch --set-upstream 1的语法的快捷方式是:

git push -u origin my_branch

…你第一次推送那个分支。或者,从同名分支推送到当前分支(对于别名很方便):

git push -u origin HEAD

您只需要使用-u一次,就可以在您的分支和 at 分支之间建立关联,origin方式与此相同git branch --set-upstream

就个人而言,我认为必须在您的分支和远程分支之间明确建立关联是一件好事。很遗憾 和 的规则不同git push``git pull


1这可能听起来很傻,但我经常忘记指定当前分支,假设这是默认值 - 它不是,结果最令人困惑。

2012-10-11 更新:显然我不是唯一一个容易出错的人!感谢VonC指出 git 1.8.0 引入了更明显的git branch --set-upstream-to,如果您在分支上,可以按如下方式使用my_branch

git branch --set-upstream-to origin/my_branch

…或使用短选项:

git branch -u origin/my_branch

此更改及其原因在git 1.8.0 的发行说明中进行了描述,候选版本 1

很容易说git branch --set-upstream origin/master,但这告诉 Git 安排本地分支origin/master与当前签出的分支集成,这不太可能是用户的意思。该选项已弃用;改用新的--set-upstream-to(带有 short-and-sweet -u)选项。

2022-02-18
小编典典

你可以用更少的打字来实现这一点。首先,改变你的推送方式:

git config --global push.default current

这将推断出origin my_branch零件,因此您可以这样做:

git push -u

这将创建具有相同名称的远程分支并跟踪它。

2022-02-18