【问题标题】:git: fatal: Cannot switch branch to a non-commit '12382'git:致命:无法将分支切换到非提交“12382”
【发布时间】:2015-09-05 07:00:22
【问题描述】:

我团队中的其他人创建了一个新的 git 分支,提交并推送到我们使用的常用遥控器。当我尝试检查这个分支时,我得到了这个:

% git checkout 12382
fatal: Cannot switch branch to a non-commit '12382'

我从这个存储库中检查其他分支没有遇到任何问题;尝试在此之后立即检查另一个(我没有本地副本的那个),它工作正常。

我尝试在我们的 Go 管道上用这个分支构建一个服务器,它工作正常 - 这意味着服务器成功地检查了那个分支。

试过这个来检查事物的状态:

% git remote show origin
* remote origin
  Fetch URL: git@gitlab.mycompany.com:mycompany/myrepository.git
  Push  URL: git@gitlab.mycompany.com:mycompany/myrepository.git
  HEAD branch: stage
  Remote branches:
    10112                     tracked
    10198                     tracked
    10678                     tracked
...
    12382                     tracked    <<<---
...
  Local branches configured for 'git pull':
...
  Local refs configured for 'git push':
...

谁能建议如何解决这个问题?出了什么问题?

【问题讨论】:

    标签: git git-checkout


    【解决方案1】:

    Git 很困惑,因为 12382 看起来像一个提交哈希。使用完全限定名称签出分支:

    git checkout refs/heads/12382 --
    

    或者,如果是远程分支:

    git checkout refs/remotes/origin/12382 --
    

    【讨论】:

    • 感谢您的回答,但我们所有的分支都是 5 位数字,看起来像散列,这一直很好用。刚刚尝试了您建议的命令,给出了“错误:pathspec 'refs/heads/12382' 与 git 已知的任何文件不匹配。”
    • 试试git checkout refs/heads/12382 --。另外,假设你还没有本地分支,你必须使用refs/remotes/origin/12382(也许只是origin/12382也可以)
    • 等一下,远程命令似乎有效:git checkout refs/remotes/origin/12382
    • 这对我不起作用。我收到错误:refs/heads/12382 和 refs/heads/origin/12382 的 pathspec 我最终得到了一个分离的 HEAD。
    • 这个答案的附录:与您的贡献者讨论使用不太钝的分支名称
    【解决方案2】:

    @knittl:感谢成功,必须执行以下附加步骤:

    % git checkout refs/remotes/origin/12382
    Note: checking out 'refs/remotes/origin/12382'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
      git checkout -b new_branch_name
    
    HEAD is now at 2d834e4... 
    
    % git branch | grep 12382
    * (detached from origin/12382)
    
    % git checkout -b 12382
    Switched to a new branch '12382'
    
    % git status
    On branch 12382
    nothing to commit, working directory clean
    
    % git push --set-upstream origin 12382
    Branch 12382 set up to track remote branch 12382 from origin.
    Everything up-to-date
    

    【讨论】:

    • 你可以一步完成git checkout -b 12382 --track refs/remotes/origin/12382 ;)
    【解决方案3】:

    这个问题是一个边缘案例,它已经得到了回答。

    我会在更一般的层面上回答错误:


    为了能够切换/签出源代码树中的某些内容,它必须是以下类型:

    • commit : git checkout: 90392aeda17d730d472493bc5a36237407c80979 或者只是执行前 7 位 ``git checkout: 90392ae`
    • 标记git checkout V2.0.3
    • 分支(远程分支也是)git checkout newLogin
    • git checkout HEAD^1
    • 哈希,短哈希。

    所以如果你切换到它们都不是的东西,比如你输入错误的分支名称,git 会给你这个错误。

    Cannot switch branch to a non-commit 表示您认为您正在尝试签出不是tree-ish

    【讨论】:

      【解决方案4】:
      % git switch -t origin/12382
      Branch '12382' set up to track remote branch '12382' from 'origin'.
      Switched to a new branch '12382'
      

      这似乎对我有用。我曾经使用git checkout,但我现在可能会开始使用git switch,来更改分支。

      在我的例子中,有人使用错误跟踪系统票号作为没有字母前缀的分支名称。

      git switch 从 git 2.23 开始。

      【讨论】:

        【解决方案5】:

        虽然老问题我没有在网上找到上帝的答案,所以在下面添加对我有用的: git checkout --track origin/

        【讨论】:

          猜你喜欢
          • 2020-12-27
          • 1970-01-01
          • 2018-01-23
          • 1970-01-01
          • 2011-10-07
          • 2016-11-12
          • 2016-03-29
          • 2020-11-30
          • 2019-12-14
          相关资源
          最近更新 更多