使用 git reset --hard "commit id",现在我的 HEAD 分离了

Used git reset --hard "commit id", and now my HEAD is detached

我对 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*

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

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

感谢任何帮助。自过去 5 个小时以来,我一直在努力解决这个问题。

I then deleted my merge branch, and proceeded to create a new merge branch off of remote main branch (origin/main)

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

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

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