我在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的别名的人,我使用的是:

alias up="git branch | awk '/^\\* / { print \$2 }' | xargs -I {} git branch --set-upstream-to=origin/{} {}"

现在,只要您得到:

$ git pull
There is no tracking information for the current branch.
...

只需运行:

$ up
Branch my_branch set up to track remote branch my_branch from origin.
$ git pull

你可以走了

其他回答

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”的行为),并且还希望设置上游跟踪,则此选项非常有用。最有可能从该选项中受益的工作流是“简单”的中心工作流,其中所有分支在远程上都应具有相同的名称。

这里有一个gitpush的bash别名,它在每次推送时都可以安全运行,并且会自动在第一次推送设置上游和之后执行正常推送之间切换。

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'

原始帖子

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

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

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

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

git po

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

这是我最常用的“操”。

$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

$ fuck
git push --set-upstream origin master [enter/↑/↓/ctrl+c]
Counting objects: 9, done.
...

此外,在终端中键入脏话也很有趣。