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


当前回答

噢,我的zsh的git插件已经将其别名为gpsup。这将推送并设置分支的上游。一气呵成!

我个人喜欢标准化和一致的解决方案。建议其他人使用相同的别名。:)

其他回答

您还可以明确地告诉gitpull要拉哪个远程分支(正如错误消息中提到的那样):

git pull<remote name><remote branch>

但是,要小心:如果您在不同的分支上执行显式拉取,那么您拉取的refspec将被合并到您所在的分支中!

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

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

$ git checkout --track origin/somebranch

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

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

通过运行以下git命令之一,可以让git自动创建上游分支

git-config--global--添加push.default电流git-config--global--add push.autoSetupRemote true

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

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

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

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