首页 > 解决方案 > 使用 git reset --hard "commit id",现在我的 HEAD 已分离

问题描述

我对 git 比较陌生。

问题:只想将我的远程主分支与合并分支合并。

我想将我的远程主分支与我正在处理的合并分支同步:合并分支在主分支后面有 80 个提交。由于某种原因,我遇到了一些冲突,自动合并失败。

然后我尝试切换到我的远程主分支(git checkout origin/main),它也失败了,建议我先解决冲突。我决定简单地回到之前的提交,为此我使用了:

git reset --hard *commit id*

然后我删除了我的合并分支,并继续从远程主分支(origin/main)创建一个新的合并分支。这就是问题的开始。我写的

git checkout origin/main

我收到了这个

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 *commit id* *Description*

我尽我所能,阅读文章,阅读 stackoverflow 问题,甚至删除并克隆了我的本地存储库,但不知何故无法将 HEAD重新附加到远程分支,如果可以的话。

有什么我在这里想念的吗?我想要的只是使用 git checkout origin/main 它应该指向分支而不是特定的提交 ID。

任何和所有的帮助表示赞赏。自从过去 5 个小时以来,我一直在努力解决这个问题。

标签: gitgithubversion-controlgit-branch

解决方案


然后我删除了我的合并分支,并继续从远程主分支 (origin/main) 创建一个新的合并分支

这实际上不是你所做的。git checkout origin/main检查了引用的提交origin/main,因此最终处于分离的头部状态。

你需要做的是git checkout -b merge origin/main。这将有效地创建本地merge分支origin/main并检查它。

如果您当前处于分离头状态,您应该能够执行上述命令来解决您的问题。


推荐阅读