分离的头不会因创建分支而消失。为什么?

Detached head does not disappear by creating branch. Why?

不知道为什么,我总是陷入超然的状态。并且仍然无法找到摆脱它的好方法。有一段时间我在想在分离的头中创建分支就足够了,但这无济于事,HEAD 仍然分离:

>git branch
 (HEAD detached at af34e34)
 master

>git branch detachedhead20160610

>git branch
 (HEAD detached at af34e34)
 detachedhead20160610
 master

>git branch detachedhead20160610
Fatal: A branch named 'detachedhead20160610' already exists.

>git branch detachedhead20160610-2

>git branch
 (HEAD detached at af34e34)
 detachedhead20160610
 detachedhead20160610-2
 master

即刚刚添加的新分支没有固化分离头状态。

您的困惑是因为不知道 git branch 命令的作用。来自 documentation:

Note that this will create the new branch, but it will not switch the working tree to it; use "git checkout " to switch to the new branch.

换句话说,当您git branch detachedhead20160610时,您创建了一个名为detachedhead20160610的新分支,但您没有切换 到那个分支,因此 Git 仍然报告你处于分离的头部状态。相反,您有两个选择:

git branch detachedhead20160610      # create a new branch
git checkout detachedhead20160610    # switch to that branch

或者您可以通过一个命令完成:

git checkout -b detachedhead20160610 # create and switch to new branch