Chances are if you’ve been using Git as your source control weapon of choice, you’ve encountered the detached head issue at some point. I’ve seen even long time users of Git get stumped on this issue.
So onto the solution, how the heck do you get everything back to normal? We are going to dive into the terminal for this one, I am not sure if there are any Git GUI’s out there like GitKraken or Sourcetree that can handle these scenarios.
- Create a branch called “temp” by typing:
git branch temp
- Switch over to your new branch by checking it out:
git checkout temp
- Point the master pointer to the temp branch pointer (the-f means force):
git branch -f master temp
- Switch back to the
git checkout master
- Now we delete our temp branch:
git branch -d temp
- Push our new changes to the remote repository:
git push origin master
Provided nothing else messed up, things should be back to normal now.
Nice, that really helped me a lot!
Between step 3 and step 4 you need to do a “git checkout master” as you the git command in step 4 would give you “error: Cannot delete the branch ‘temp’ which you are currently on.”
Also: what Mickey said.
This is not working. I still have detached head!
Between step-3 and 4, we have to switch branch and then delete.