我今天结束了一个分离的头,同样的问题描述:git推送说所有最新的,即使我有局部的变化

据我所知,我没有做任何不同寻常的事情,只是从本地回购中提交和推送。

那么,我是如何得到一个分离的头部的呢?


当前回答

一个简单的偶然的方法是做一个git签出头作为head的拼写错误。

试试这个:

git init
touch Readme.md
git add Readme.md
git commit
git checkout head

这给了

Note: checking out 'head'.

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 9354043... Readme

其他回答

分离HEAD意味着当前签出的不是本地分支。

导致分离HEAD状态的一些场景:

If you checkout a remote branch, say origin/master. This is a read-only branch. Thus, when creating a commit from origin/master it will be free-floating, i.e. not connected to any branch. If you checkout a specific tag or commit. When doing a new commit from here, it will again be free-floating, i.e. not connected to any branch. Note that when a branch is checked out, new commits always gets automatically placed at the tip. When you want to go back and checkout a specific commit or tag to start working from there, you could create a new branch originating from that commit and switch to it by git checkout -b new_branch_name. This will prevent the Detached HEAD state as you now have a branch checked out and not a commit.

try

git reflog 

这给了你一个HEAD和分支指针的历史 哪里搬过去了。

例如:

88ea06b HEAD@{0}: checkout:从DEVELOPMENT移动到remotes/origin/SomeNiceFeature e47bf80 HEAD@{1}:拉动原点发展:快进

这个列表的顶部是一个原因,人们可能会遇到一个分离的头 状态……检查一个远程跟踪分支。

如果不能使用git开关,使用'git checkout -b [branch]'而不是'git checkout [branch]'

分离的头

HEAD是一个指向当前签出分支或提交的指针,它回答了一个问题:我现在在存储库的哪个位置?HEAD可以处于附加(默认)或分离两种状态之一,这取决于您是否签出了本地分支。

我的头是怎么脱落的?

操作比让HEAD处于分离状态

最终进入分离的HEAD状态可能是由于各种原因造成的,以下是5种常见的情况(也许你做过以下任何一种):

使用哈希表签出一个特定的提交。 $ git checkout 14ko3 显式地签出一个远程分支,即。 $ git checkout origin/master 使用分离标志切换到一个分支(Git 2.23),即 $ git主开关——分离 签出一个标签,即 $ git checkout v1.0.1 执行一个交互式的重基(或者包含冲突变更的常规重基),例如: $ git rebase master特性-1 -交互式

在分离状态下,可以在不影响任何现有分支的情况下进行实验更改。请参见下面的信息图,说明在附加状态和分离状态下提交的区别。

一个常见的误解是,消息You are in 'detached HEAD' state是错误的语气,而实际上它只是描述HEAD是如何引用当前快照的。

从分离到依附的状态

要从分离状态切换到附加状态,您可以从所在的位置创建一个新分支,或者切换回现有的分支。

注意:如果你切换到另一个现有的分支,在分离状态下创建的任何提交最终(垃圾收集后)都会被丢弃,而不是首先在一个新的分支中持久化你的更改。

来源:以上节选自这篇关于主题的完整文章:Git中的HEAD是什么?

进入git分离头状态的另一种方法是尝试提交到远程分支。喜欢的东西:

git fetch
git checkout origin/foo
vi bar
git commit -a -m 'changed bar'

请注意,如果您这样做,任何进一步尝试签出origin/foo将使您回到分离的头部状态!

解决方案是创建自己的本地foo分支来跟踪origin/foo,然后可选地push。

这可能与您最初的问题无关,但该页在“git分离头”的谷歌次点击中排名靠前,这个场景的文档记录严重不足。