git:致命:无法将分支切换到非提交“12382”

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

我团队中的其他人创建了一个新的 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 很困惑,因为 12382 看起来像一个提交哈希。使用完全限定名称签出分支:

git checkout refs/heads/12382 --

或者,如果是远程分支:

git checkout refs/remotes/origin/12382 --

@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

这个问题是一个边缘案例,已经有人回答了。

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


为了能够 switch/checkout 到源代码树中的某些内容,它必须是以下类型:

  • 提交:git checkout: 90392aeda17d730d472493bc5a36237407c80979 或者可能只执行前 7 digits ``git checkout: 90392ae`
  • 标签git checkout V2.0.3
  • 分行(也有远程分行)git checkout newLogin
  • git checkout HEAD^1
  • 哈希,短哈希。

因此,如果您要切换到其中 none 个,就像您输入错误的分支名称一样,git 会给您这个错误。

无法将分支切换到 non-commit 意味着认为您正在尝试结帐到不是 tree-ish

的内容
% 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.

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