如何告诉Git始终拉取主分支?

21
我发现 Git 的文档对这个问题非常晦涩难懂。我想做一件简单的事情,但似乎做起来并不简单。
我有以下情况:
$ git remote -v
origin  git://192.168.0.49/mnt/repos
stick   /mnt/titanium/podaci/repos

我可以使用git pull命令从远程仓库获取并合并代码,这个命令非常好用:

$ git pull
Already up-to-date.

我可以像这样从棍子中拉取:

$ git pull stick master
Already up-to-date.

然而,当我从没有 master 部分的 stick 中拉取时,我收到了这条消息:

$ git pull stick
From /mnt/titanium/podaci/repos
 * [new branch]      su2009  -> stick/su2009
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me either.  Please
name which branch you want to merge on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details on the refspec.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

    branch.master.remote = <nickname>
    branch.master.merge = <remote-ref>
    remote.<nickname>.url = <url>
    remote.<nickname>.fetch = <refspec>

See git-config(1) for details.

这里有几件事情让我感到困惑。"你的配置文件"在这里是指什么?我应该编辑哪个文件,具体应该输入什么内容?在这种情况下昵称是什么意思?

我本来期望我要完成的任务很常见,但是我没有找到一个直截了当的、带有示例的答案。

1个回答

26

这里的“配置文件”是什么意思?

指的是你的存储库中位于根目录下的.git/config文件。 (还有一个每个用户的全局配置文件~/.gitconfig,但您不应该将存储库特定的设置放在那里。)

我应该编辑哪个文件,并且要输入什么内容?

您可以使用git config程序编写配置信息,而不是手动输入。但是,如果您想手动操作,只需打开.git/config -- 语法相当简单。

这种情况下的“昵称”是什么意思?

在这种情况下,“昵称”是远程的名称 - 因此为“stick”。 您无需担心remote.*选项,因为这些选项已经设置好了,但您需要设置branch.*选项。 这些选项告诉Git在从“stick”拉取时要合并什么。

假设您想在从“stick”拉取时合并来自“stick”的主分支。 您可以像这样执行:

# Sets stick as default remote for git pull.
# Note that origin will no longer be the default remote for git pull!
$ git config branch.master.remote stick

# Automatically merge in stick's master branch when doing a git pull
$ git config branch.master.merge refs/heads/master

现在,当你没有指定remote或refspec信息时执行git pull命令,它将从stick获取所有分支并合并stick的主分支。请注意,origin不再是默认的远程库;如果要合并origin的主分支,你需要使用git pull origin master命令。

如果你不想改变默认的远程库为stick,你需要继续使用git pull stick master命令。


1
你最后一句话很关键。我希望有办法绕过这个问题。 - Milan Babuškov
我目前不知道一个 -- branch.<nickname>.remotebranch.<nickname>.merge 是相辅相成的,每个分支只能有一个 remote 列表。 - mipadi
嗯,我想这就是Git的工作方式。如果它让我太烦了,也许我会向开发人员提交一个功能请求(但这不太可能发生)。回答已被接受。 - Milan Babuškov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接