首页 > 解决方案 > 如何让 git 按名称自动匹配远程分支?

问题描述

我经常使用克隆的存储库,具有“来源”(我的)和“上游”(原始来源)。我总是从 github 克隆“原始”存储库以进行处理并创建 PR,并且不时需要从“上游”拉取以与那里正在进行的最新更改同步。

git clone <origin uri>我能做到之后

git push/pull

没有指定分支,因为它已经被跟踪;但是在另一个遥控器上这样做

git pull upstream

当例如在master分支上时,我希望 git 实际执行

git pull upstream master

代替:

You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

我知道我可以配置跟踪远程,但我想知道这是否可以是自动的,只要分支名称相同。

标签: git

解决方案


您可以使用以下配置,因此每当您从远程签出新分支时,它将被自动跟踪,而无需您使用--set-upstream-to或设置跟踪--track

git config --global branch.autosetupmerge true

推荐阅读