【问题标题】:Why won't Git let me check out a branch from an alternate remote?为什么 Git 不让我从备用远程检查分支?
【发布时间】:2018-09-13 02:34:15
【问题描述】:

我正在做一个有两个遥控器的项目,一个叫origin,一个叫vsts。默认遥控器是origin。这是git remote -v 的输出,其中一些部分匿名为***

$ git remote -v origin git@bitbucket.org:***/***.git (fetch) origin git@bitbucket.org:***/***.git (push) vsts ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** (fetch) vsts ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** (push)

我正在尝试从vsts 签出一个新分支。它被称为release/1.4.1。我在 Git 2.16.x 上,所以我应该可以使用 git checkout,但会发生这种情况:

$ git checkout release/1.4.1 error: pathspec 'release/1.4.1' did not match any file(s) known to git.

我想可能是假设我的意思是origin。所以我试试这个:

$ git checkout vsts/release/1.4.1 error: pathspec 'vsts/release/1.4.1' did not match any file(s) known to git.

我应该确保 git 可以找到分支。所以我使用git ls-remote 来获取远程分支的列表:

$ git ls-remote vsts ... abcde*** refs/heads/release/1.4.1 ...

我得到一个分支列表和提交哈希,release/1.4.1 绝对是其中之一。

我尝试了更多的东西:

$ git checkout -b release/1.4.1 vsts/release/1.4.1 fatal: 'vsts/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

$ git fetch vsts release/1.4.1 From ssh://vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** * branch release/1.4.1 -> FETCH_HEAD

(这个命令之后,我把前面的都试了一遍,结果还是没变。)

$ git checkout -b release/1.4.1 remotes/vsts/release/1.4.1 fatal: 'remotes/vsts/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

$ git checkout -b release/1.4.1 remotes/vsts/refs/heads/release/1.4.1 fatal: 'remotes/vsts/refs/heads/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

如果我尝试git pull vsts/release/1.4.1,它会成功地将远程分支release/1.4.1 合并到当前分支中,但这不是一个有用的解决方法。

我还能尝试什么?我不明白为什么我不能签出远程分支。

【问题讨论】:

    标签: git git-checkout git-remote


    【解决方案1】:

    问题是我的本地 git 配置搞砸了。我用git config --local -e在vim中打开,找到了这个部分:

    [remote "vsts"] url = ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** fetch = +refs/heads/dev:refs/remotes/vsts/dev

    看起来它只是设置为获取dev。我不知道它是怎么变成这样的,但我将其更改为以下内容:

    [remote "vsts"] url = ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** fetch = +refs/heads/*:refs/remotes/vsts/*

    之后我可以这样做:

    $ git checkout release/1.4.1 Switched to a new branch 'release/1.4.1' Branch 'release/1.4.1' set up to track remote branch 'release/1.4.1' from 'vsts'.

    【讨论】:

    • 我也不太清楚它是怎么做到的,但这是正确的解决方法。请注意,git remote add 添加了一个额外的遥控器,它允许-t <branch> 将获取限制到一个特定的分支。如您所见,它通过编写fetch = refspec 来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2016-09-25
    • 2021-07-10
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    相关资源
    最近更新 更多