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

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

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


当前回答

分离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}:拉动原点发展:快进

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

当你签出到一个commit git checkout <commit-hash>或一个远程分支时,你的HEAD将被分离,并尝试在上面创建一个新的提交。

任何分支或标记都无法访问的提交将在30天后被垃圾收集并从存储库中删除。

解决这个问题的另一种方法是为新创建的提交和签出创建一个新分支。Git checkout -b <branch-name> <commit-hash>

本文将说明如何进入分离HEAD状态。

一个简单的偶然的方法是做一个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

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

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

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

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

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

如果git要重命名分离的HEAD,我会让它命名为一个没有被分支标识的HEAD,并且很快就会被忘记。

我们作为人可以很容易地记住分支机构的名称。我们做git checkout new-button-feature / git checkout main。主按钮和新按钮功能易于记忆。我们可以用gitbranch得到所有分支的列表。但如果只是提交,你必须做git reflog,这非常乏味。因为你有成千上万的提交,但只有很少的分支。

分离提交的标识符就是它的SHA。假设你签出了一个提交(而不是一个分支),即你签出了git d747dd10e450871928a56c9cb7c6577cf61fdf31,你会得到:

注意:检查 “d747dd10e450871928a56c9cb7c6577cf61fdf31”。 你处于“分离的头部”状态。 ...

然后,如果你做了一些更改并提交,你仍然不在分支上。

你还记得commit SHA吗?你不会!

Git不希望这种情况发生。因此,它通知您的HEAD没有关联到一个分支,因此您更倾向于签出一个新的分支。结果在该消息下面还说:

如果您想创建一个新的分支来保留您所创建的提交,您可以 可以(现在或以后)再次使用-b和签出命令。 例子: Git checkout -b


为了深入一点,分支以一种聪明的方式构建。当你提交时,它会更新它的HEAD。另一方面,标签并不是这样的。如果签出一个标签,那么你又在一个分离的HEAD上。主要的原因是,如果你从那个标签做出一个新的提交,那么这个提交没有被任何东西(不是任何分支或标签)引用,那么它仍然被认为是一个分离的HEAD。

只有当你在一个分支上时,附加的正面才会发生。

更多信息请看这里

HEAD是一个指针,它直接或间接地指向 特殊的提交: 附加HEAD意味着它附加到某个分支(即它 指向一个分支)。 分离的头意味着它没有附着在任何分支,即它 直接指向一些提交。

从另一个角度看,如果你在树枝上做cat .git/HEAD,你会得到:

ref: refs/heads/Your-current-branch-name

然后如果你使用cat refs/heads/ your -current-branch-name,那么你也会看到你的分支所指向/引用的提交的SHA。

然而,如果你在一个分离的HEAD上,你和cat .git/HEAD只会得到提交的SHA,没有别的:

639ce5dd952a645b7c3fcbe89e88e3dd081a9912

我的意思是它的头没有指向任何分支。它只是直接指向一个提交。


As a result of all this, anytime you checkout a commit (without using the branch name to checkout), even if that commit was the latest commit of your main branch, you're still in a detached HEAD because your HEAD is not pointing to any of your local branches. Hence even checking out a tag will put you in a detached HEAD. To add onto that, even checking out a remote branch that you have fetched into your computer would result in a detached head ie git checkout origin main would also end up as a detached head...

总结

以下所有情况都会导致头部脱落:

检出任何提交 签出任何标签 签出任何远程分支

如果你查过当地的分支机构,你就只在一个附属的头上


特别感谢Josh Caswell和Saagar Jha帮助我解决这个问题。