无法使用多个远程仓库进行检出操作

35

我在本地Git仓库中设置了两个遥控器。一个是我正在为其做出贡献的开源项目的仓库,另一个是该仓库的我的分支。

似乎我只能检出从origin远程下载的内容。我通常从远程获取分支的方法如下:

$ git fetch <remote> <branch>
$ git checkout <branch>

但是在我的当前情况下,这似乎行不通。

$ git fetch my-remote my-branch
remote: Counting objects: 2297, done.
remote: Compressing objects: 100% (1951/1951), done.
remote: Total 2297 (delta 1044), reused 0 (delta 0), pack-reused 50
Receiving objects: 100% (2297/2297), 2.10 MiB | 1.59 MiB/s, done.
Resolving deltas: 100% (1045/1045), done.
From https://github.com/me/my-repo
 * branch            my-branch -> FETCH_HEAD
 * [new branch]      my-branch -> origin/my-branch
$ git checkout my-branch
error: pathspec 'my-branch' did not match any file(s) known to git.

而且,当我执行git branch时,该分支并不存在。

这是怎么回事?


1
git branch --all 命令告诉你什么? - ckruczek
1
使用Git 2.19(2018年第三季度),您将可以避免该错误消息,使用新的配置checkout.defaultRemote=origin。请参见下面的答案 - VonC
3个回答

54

当你只有一个远程仓库(我们称其为 origin)时,当你输入

git checkout foo

如果 foo 不存在但 origin/foo 存在,则 Git 会表现得好像你输入了以下命令

git checkout -b foo origin/foo

如果你有多个远程仓库,而本地并没有存在 foo,但是在两个或更多的远程仓库中存在 foo,则这种行为将被抑制。

您需要显式地创建 foo 并告诉 Git 您要跟踪哪个远程分支。

git checkout -b foo <remote>/foo


我遇到了一个问题,我在 git 的 .config 文件中手动复制了远程节点,因为它是另一个节点的镜像,并将其重命名。但我忘记了在 fetch= 中也重命名 refs,这导致了问题的出现。 - Renato Chencinski

12
Git 2.19将有所帮助,因为"git checkout"和"git worktree add"在自动创建本地分支时学会了尊重checkout.defaultRemote,当一个仓库有多个远程仓库并且这些远程仓库有共享同名跟踪分支时。

请查看以下提交记录:commit 8d7b558, commit ad8d510, commit 1c55055, commit 3c87aa9, commit e4d2d55, commit e417151, commit 17b44ae, commit c8cbf20 (2018年6月5日),作者为Ævar Arnfjörð Bjarmason (avar)
(在2018年8月2日,由Junio C Hamano -- gitster --合并到commit 50858ed)

注意:DWIM 是指“做我所想”,当计算机系统尝试预测用户意图时,自动纠正微不足道的错误,而不是盲目执行用户明确但有可能不正确的输入。
我在 Git 2.16 远程格式Git 2.13 checkout 完成 中见过它。

checkout 和 worktree:引入 checkout.defaultRemote

Introduce a checkout.defaultRemote setting which can be used to designate a remote to prefer (via checkout.defaultRemote=origin) when running e.g. "git checkout master" to mean origin/master, even though there's other remotes that have the "master" branch.

I want this because it's very handy to use this workflow to checkout a repository and create a topic branch, then get back to a "master" as retrieved from upstream:

(
   cd /tmp &&
   rm -rf tbdiff &&
   git clone git@github.com:trast/tbdiff.git &&
   cd tbdiff &&
   git branch -m topic &&
   git checkout master

)

That will output:

Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'master'

But as soon as a new remote is added (e.g. just to inspect something from someone else) the DWIMery goes away:

(
   cd /tmp &&
   rm -rf tbdiff &&
   git clone git@github.com:trast/tbdiff.git &&
   cd tbdiff &&
   git branch -m topic &&
   git remote add avar git@github.com:avar/tbdiff.git &&
   git fetch avar &&
   git checkout master

)

Will output (without the advice output added earlier in this series):

error: pathspec 'master' did not match any file(s) known to git.

The new checkout.defaultRemote config allows me to say that whenever that ambiguity comes up I'd like to prefer "origin", and it'll still work as though the only remote I had was "origin".

CodeManX指出在评论中如何设置新选项:

git config --add checkout.defaultRemote origin 

(add --global if you want to set it globally)


4
在 Git 2.28 中,我尝试运行 git config checkout.defaultRemote=origin,但出现了错误:_error: invalid key: checkout.defaultRemote=origin_。不过,下面这个命令可以正常工作:git config --add checkout.defaultRemote origin(如果您想要全局设置,请添加 --global)。 - CodeManX
@CodeManX 谢谢你。我已经将你的评论包含在答案中,以增加可见性。 - VonC
1
git config --add checkout.defaultRemote upstream 拯救了我的生命。每次我都会掉进同样的坑里,现在我再也不会了! - Victor

8
  1. when there is only one remote,the git checkout of a remote branch works perfectly fine.If the branch is not in the local machine,it checks for the branch in the git website and if the branch is there in the git website,it will download the branch into your local machine and then set it to track the branch in the branch in the git website(also called as remote)
  2. But when you add two remotes,git checkout of a remote branch using git checkout branch fails.
  3. This can be fixed by setting one of the remote as default.
  4. For doing this,add the below line to your gitconfig file.(Global git config file is usually located at ~/.gitconfig)

    [checkout]
        defaultRemote=origin
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接