我今天结束了一个分离的头,同样的问题描述: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帮助我解决这个问题。

其他回答

当你签出到一个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

try

git reflog 

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

例如:

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

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

如果您试图通过重新签出文件而撤消所做的更改,并且语法不完全正确,则很容易发生这种情况。

你可以看看git日志的输出——你可以把上次成功提交后的日志尾部粘贴到这里,我们都可以看到你做了什么。或者你可以粘贴它,然后在freenode IRC上的#git中询问。

任何不是分支名称的提交签出都会得到一个分离的HEAD。SHA1表示树枝的顶端,仍然给出一个分离的HEAD。只有签出本地分支名称才能避免这种模式。

参见使用分离的HEAD进行承诺

当分离HEAD时,除了没有更新命名分支外,提交工作正常。(您可以将其视为一个匿名分支。)

例如,如果您签出一个“远程分支”而没有首先跟踪它,您可能会得到一个分离的HEAD。

见git:开关分支没有分离头

意思是:git签出origin/main(或者以前的origin/master)会导致:

Note: switching to 'origin/main'.

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 switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at a1b2c3d My commit message

这就是为什么你不应该再使用git checkout,而应该使用新的git switch命令。

使用git switch,同样尝试“签出”(切换到)远程分支将立即失败:

git switch origin/main
fatal: a branch is expected, got remote branch 'origin/main'

添加更多git开关:

使用Git 2.23(2019年8月),你不必再使用令人困惑的Git checkout命令了。

git switch也可以签出一个分支,并获得一个分离的HEAD,除了:

它有一个显式的—detach选项

在不创建新分支的情况下提交HEAD~3进行临时检查或实验: git开关——分离HEAD~3 HEAD现在在9fc9555312合并分支cc/shared-index-permbits

它不能错误地分离远程跟踪分支

See:

C:\Users\vonc\arepo>git checkout origin/master
Note: switching to 'origin/master'.

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 switching back to a branch.

Vs.使用新的git switch命令:

C:\Users\vonc\arepo>git switch origin/master
fatal: a branch is expected, got remote branch 'origin/master'

如果你想创建一个新的本地分支来跟踪一个远程分支:

git switch <branch> 

如果没有找到<分支>,但是在恰好一个远程(称其为<远程>)中存在一个具有匹配名称的跟踪分支,则将其等效于 Git开关-c <分支>——track <远程>/<分支>

别再犯错了! 没有更多不想要的分离头!

如果你使用git switch <tag>而不是git switch——detach <tag>, git 2.36会帮助你记住缺失的——detach选项。