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

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

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


当前回答

以下是VonC的评论,这是我如何解决这个“分离的头部”问题的简短版本。

在我的遥控器上创建了一个分支;来源/ dev /功能 在我的本地运行git fetch,所以现在我的本地将知道这个新的远程分支 现在运行git switch feature/dev,我们就完成了!

其他回答

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状态。

任何不是分支名称的提交签出都会得到一个分离的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选项。

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

如果您有一个与分支同名的标记,就会发生这种情况。

例如:如果“release/0.1”是标签名,那么

git checkout release/0.1

在“release/0.1”处产生分离的HEAD。如果您期望release/0.1是一个分支名称,那么您会感到困惑。