为什么我的 Git 存储库进入分离的 HEAD 状态? [英] Why did my Git repo enter a detached HEAD state?

查看:40
本文介绍了为什么我的 Git 存储库进入分离的 HEAD 状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天结束了一个分离的头,与描述相同的问题:

含义:git checkout origin/main(或origin/master过去)会导致:

注意:切换到'origin/main'.您处于分离头"状态.你可以环顾四周,做实验更改并提交它们,您可以放弃您在此所做的任何提交通过切换回分支而不会影响任何分支的状态.如果你想创建一个新分支来保留你创建的提交,你可以通过将 -c 与 switch 命令一起使用来执行此操作(现在或以后).例子:git switch -c <新分支名称>或者使用以下命令撤消此操作:git 开关 -通过将配置变量advice.detachedHead 设置为false 来关闭此建议HEAD 现在在 a1b2c3d 我的提交信息

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

使用git switch,同样尝试checkout"(切换到)远程分支会立即失败:

git switch origin/main致命:需要一个分支,得到远程分支'origin/main'


要在 git switch 上添加更多内容:

使用 Git 2.23(2019 年 8 月),您不必使用 令人困惑的 git checkout 命令 了.

git switch 也可以结帐分支,并且得到一个分离的 HEAD,除了:

  • 它有一个显式的 --detach 选项
<块引用>

要检查提交 HEAD~3 以进行临时检查或实验而不创建新分支:

git switch --detach HEAD~3HEAD 现在位于 9fc9555312 合并分支 'cc/shared-index-permbits'

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

见:

C:Usersvoncarepo>git checkout origin/master注意:切换到源/主".您处于分离头"状态.你可以环顾四周,做实验更改并提交它们,您可以放弃您在此所做的任何提交通过切换回分支而不会影响任何分支的状态.

对比.使用新的 git switch 命令:

C:Usersvoncarepo>git switch origin/master致命:需要一个分支,得到远程分支'origin/master'

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

git switch 

<块引用>

如果 未找到,但在一个具有匹配名称的远程(称为 )中确实存在跟踪分支,则处理相当于

git switch -c --track <远程>/<分支>

不要再犯错了!
不再有多余的分离 HEAD!

I ended up with a detached head today, the same problem as described in: git push says everything up-to-date even though I have local changes

As far as I know I didn't do anything out of the ordinary, just commits and pushes from my local repo.

So how did I end up with a detached HEAD?

解决方案

Any checkout of a commit that is not the name of one of your branches will get you a detached HEAD. A SHA1 which represents the tip of a branch still gives a detached HEAD. Only a checkout of a local branch name avoids that mode.

See committing with a detached HEAD

When HEAD is detached, commits work like normal, except no named branch gets updated. (You can think of this as an anonymous branch.)

For example, if you checkout a "remote branch" without tracking it first, you can end up with a detached HEAD.

See git: switch branch without detaching head

Meaning: git checkout origin/main (or origin/master in the old days) would result in:

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

That is why you should not use git checkout anymore, but the new git switch command.

With git switch, the same attempt to "checkout" (switch to) a remote branch would fail immediately:

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


To add more on git switch:

With Git 2.23 (August 2019), you don't have to use the confusing git checkout command anymore.

git switch can also checkout a branch, and get a detach HEAD, except:

  • it has an explicit --detach option

To check out commit HEAD~3 for temporary inspection or experiment without creating a new branch:

git switch --detach HEAD~3
HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'

  • it cannot detached by mistake a remote tracking branch

See:

C:Usersvoncarepo>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. using the new git switch command:

C:Usersvoncarepo>git switch origin/master
fatal: a branch is expected, got remote branch 'origin/master'

If you wanted to create a new local branch tracking a remote branch:

git switch <branch> 

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

git switch -c <branch> --track <remote>/<branch>

No more mistake!
No more unwanted detached HEAD!

这篇关于为什么我的 Git 存储库进入分离的 HEAD 状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆