我在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 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
}

其他回答

git branch --set-upstream-to=origin/master<branch_name>

您也可以执行gitpush-uorigin$(current_branch)

git config --global push.autoSetupRemote true

OP询问:

我了解到,我可以通过以下方式实现:gitbranch—设置上游my_branch原点/my_branch但为什么我需要为我创建的每个分支都这样做?

您不需要一直设置上游。不再(十一年后)。

在Git 2.37(2022年第三季度)中,一个Git-config--globalpush.autoSetupRemote true将为您解决这一问题。

参见Tao Klerks(TaoK)的提交05d5775、提交8a649e、提交bdaf1df(2022年4月29日)。(于2022年5月26日由Junio C Hamano--gitster在提交f49c478中合并)

push:新配置选项“push.autoSetupRemote”支持“简单”push签字人:Tao Klerks

在一些“简单”的集中工作流中,用户希望远程跟踪分支名称与本地分支名称匹配。“git push”(man)推送到分支的远程版本/实例,“git pull”(man)推送对远程分支的任何更改(同一用户在另一个地方或其他用户所做的更改)。push.default默认选项“simple”支持这种期望,该选项拒绝对不匹配的跟踪分支名称进行默认推送,而新的branch.autosetupmmerge选项“simple”仅为同名远程分支设置远程跟踪。当用户创建了一个新分支,但尚未推送(push.default未设置为“current”)时,系统会提示用户“当前分支%s没有上游分支”错误,以及如何推送和添加跟踪的说明。这个错误很有帮助,因为每个分支只遵循一次建议就可以永远解决该分支的问题,但对于“简单”的集中工作流来说,这总是正确的做法,因此最好只执行它。使用新的配置设置push.autoSetupRemote支持此工作流,当未配置远程跟踪分支时,该设置将导致默认推送到远程和上游--set上的相同名称。当遇到“当前分支%s没有上游分支”错误时,还添加一个提示,提供此新选项,并添加相应的测试。

新提示是:

为无跟踪的分支自动执行此操作上游,请参阅“git-help-config”中的“push.autoSetupRemote”

git-config现在在其手册页中包括:

push.autoSetupRemote如果设置为“true”,则假定--在没有当前分支存在上游跟踪;此选项在push.default选项“simple”、“upstream”和“current”时生效。默认情况下,如果您希望将新分支推送到默认远程(如“push.default=current”的行为),并且还希望设置上游跟踪,则此选项非常有用。最有可能从该选项中受益的工作流是“简单”的中心工作流,其中所有分支在远程上都应具有相同的名称。

我只想做这样的事:

git checkout -b my-branch
git commit -a -m "my commit"
git push

由于我没有找到更好的解决方案,我只是在~/.bashrc上创建了一个bash别名:

alias push="git push -u origin HEAD"

现在,只需执行push命令即可完成此任务(您也可以在~/.gitconfig上使用其他名称添加此别名,例如pushup)

你可以简单地

git checkout -b my-branch origin/whatever

首先。如果您将branch.atosetupmerge或branch.atostubrebase(我最喜欢的)设置为总是(默认值为true),我的分支将自动跟踪origin/whatever。

请参阅git-help-config。