Search code examples
gitgithubrebasegit-detached-head

Fix detached head without losing commits made while detached


I'm currently having a detached HEAD after doing a rebase. I did the rebase as I thought this would solve my main problem: a remote repo that thinks it's up to date with the local repo (but in fact isn't).

So here are my latest commits:

a65597d HEAD@{0}: commit: Added AutoFocus to Forms.
5405293 HEAD@{1}: commit: Register and Login with Enter functionality added.
2058612 HEAD@{2}: reset: moving to HEAD@{1}
8613cea HEAD@{3}: pull --rebase: checkout 8613ceaa282172842c2e7ace52c99ba495970b73
2058612 HEAD@{4}: commit: Added API gamedata attach function

I'm currently on no branch.

* (no branch, rebasing test)
  test
  master

What I want now is to get back to my test branch that will have the latest 2 commits which I did after the reset. How do I do this?


Solution

  • A detached head means that your HEAD is pointing to a commit rather than a reference. Normally, you would fix this by checking out a branch and you'd be done, but because you have two commits, we need to perform some very minor surgery on your repo to bring everything back in sync.

    The good news is that you've applied your new commits on top of the head of the test branch, but because of the detached head state, the pointer didn't advance. We'll just force the pointer to advance to the new location using git branch -f test HEAD

    Now that our branch is in the correct place, we can checkout the test branch git checkout test and you'll be in the right place.