我在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/mi_branch拉到my_brance,这不是很明显吗?如何将此设置为默认行为?


当前回答

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

git config --global push.default current

这将推断出原点my_branch部分,因此您可以执行以下操作:

git push -u

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

其他回答

Git v2.37.1及以上版本

如果您使用的是上述版本或更高版本,则可以使用此新的配置项自动设置远程跟踪:

git-config--全局push.autoSetupRemote true

之后,当您执行git推送跟踪时,会自动设置。无需git push-u origin my_branch


一个快捷方式,它不依赖于记住gitbranch的语法——set upstream 1是这样做的:

git push -u origin my_branch

…你第一次推那根树枝。或者,要从同名分支推送到当前分支(方便别名):

git push -u origin HEAD

您只需要使用-u一次,这将以与gitbranch-set-upstream相同的方式在分支和源分支之间建立关联。

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


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

更新2012-10-11:显然,我不是唯一一个发现容易出错的人!感谢VonC指出,git 1.8.0引入了更明显的git分支——设置上游到,如果您使用分支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/matter与当前签出的分支集成,这很可能不是用户的意思。该选项已弃用;改为使用新的--set upstream to(带有一个短而甜的-u)选项。

值得一提的是,如果您试图跟踪远程上已经存在的分支(例如,origin/somebranch),但尚未在本地签出,则可以执行以下操作:

$ git checkout --track origin/somebranch

注意:“-t”是“--track”选项的缩写版本。

这立刻建立了相同的关联。

您可以设置一个非常好的别名来处理这个问题,而无需过于冗长的语法。

我在~/.gitconfig中有以下别名:

po = "!git push -u origin \"$(git rev-parse --abbrev-ref HEAD)\""

对新分支进行提交后,只需键入以下命令即可推送新分支:

git po

因为这个问题,我重新发现了合法性(仅限OS X)。现在,我在分支时只使用以下两个命令:

合法发布将指定的分支发布到远程。(别名:pub)

合法取消发布<branch>从远程删除指定的分支。(别名:unp)

SublimeGit默认情况下具有合法支持,这使得整个分支例程像按Ctrl-b一样简单。

这里有很多很好的答案,然而,所有这些都要求您在运行git pull之前正确地执行其他操作

使用别名来完成诸如“通过创建本地正在正确跟踪的远程分支,使git push按应有的方式工作”之类的工作当然会有所帮助。然而,当你忘记使用它们,或者经历了不同的工作流程时,这些都不会对你有所帮助。

这里有一个bash函数,您可以使用它来按应该的方式进行拉取,方法是检测何时未配置远程合并目标,但远程上有一个与本地分支同名的分支,然后将该分支设置为合并目标,然后进行拉取。

git-pulldown() {
    head="$(git rev-parse --abbrev-ref HEAD)"

    if [[ $(git config "branch.$head.merge") ]]; then #there's already a merge target configured, just pull as normal from there
        git pull
    else
        if [[ $(git ls-remote --heads origin $head) ]]; then #there is an upstream branch existing with the same name as our branch
            git branch --set-upstream-to origin/$head #set merge target to upstream branch with same name
            git pull
        else #fail with explanation
            echo "Branch $head has no upstream or merge target! You will likely have to push first, or manually configure it"
            return 1
        fi
    fi
}