git:fatal:无法将分支切换为非提交'12382'

zap*_*odb 16 git git-checkout

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

% git checkout 12382
fatal: Cannot switch branch to a non-commit '12382'
Run Code Online (Sandbox Code Playgroud)

我没有遇到从这个存储库中检出其他分支的麻烦; 尝试在此之后检查另一个(一个我没有本地副本),它工作正常.

我尝试在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':
...
Run Code Online (Sandbox Code Playgroud)

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

kni*_*ttl 20

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

git checkout refs/heads/12382 --
Run Code Online (Sandbox Code Playgroud)

或者,如果它是一个远程分支:

git checkout refs/remotes/origin/12382 --
Run Code Online (Sandbox Code Playgroud)

  • 此答案的附录:与您的贡献者讨论如何使用不太钝的分支名称 (3认同)
  • 谢谢你的回答,但我们所有的分支都是 5 位数字,看起来像散列,这一直很好用。刚刚尝试了您建议的命令,给出了“错误:pathspec 'refs/heads/12382' 与 git 已知的任何文件不匹配。” (2认同)
  • 这对我不起作用。我收到错误: refs/heads/12382 和 refs/heads/origin/12382 的 pathspec 我最终得到了一个分离的 HEAD。 (2认同)

zap*_*odb 10

@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
Run Code Online (Sandbox Code Playgroud)

  • 你可以一步完成`git checkout -b 12382 --track refs/remotes/origin/12382`;) (8认同)

Chr*_*ian 5

% git switch -t origin/12382
Branch '12382' set up to track remote branch '12382' from 'origin'.
Switched to a new branch '12382'
Run Code Online (Sandbox Code Playgroud)

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

就我而言,有人使用错误跟踪系统票号作为分支名称,没有字母前缀。

git switch从 git 2.23 开始。