Home > Software engineering >  Used git reset --hard "commit id", and now my HEAD is detached
Used git reset --hard "commit id", and now my HEAD is detached

Time:10-29

I am rather new to git.

The Problem: Just wanted to merge my remote main branch with a merge branch.

I wanted to sync my remote main branch with a merge branch I was working on: The merge branch was 80 commits behind the main. For some reason, I encountered some conflicts, and the auto merge failed.

I then tried switching to my remote main branch (git checkout origin/main), and it failed too, suggesting I resolve the conflicts first. I decided to simply go back to an earlier commit, for which I used:

git reset --hard *commit id*

I then deleted my merge branch, and proceeded to create a new merge branch off of remote main branch (origin/main). This is where the problem begins. I write

git checkout origin/main

and am presented with this

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*

I did all that I could, read articles, read stackoverflow questions, even deleted and cloned my local repository but somehow couldn't manage to re-attach the HEAD to the remote branch, if you may.

Is there anything that I'm missing here? All I want is to use git checkout origin/main and it should point to the branch instead of the specific commit id.

Any and all help is appreciated. Banging my head to fix this since the last 5 hours.

CodePudding user response:

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

That's actually not what you did. git checkout origin/main checked out the commit referenced by origin/main and therefore ended up in detached head state.

What you needed to do was git checkout -b merge origin/main. This would have effectively created the local merge branch starting at origin/main and checked it out.

If you are currently in detached head state you should be able to just execute the above-mentioned command to fix your problem.

  • Related