How To Fix A Detached Head On A Git Repository

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?

  • Step 1. Create a branch called “temp” by typing: git branch temp
  • Step 2. Switch over to your new branch by checking it out: git checkout temp
  • Step 3. Point the master pointer to the temp branch pointer (the-f means force): git branch -f master temp
  • Step 4. Now we delete our temp branch: git branch -d temp
  • Step 5. Push our new changes to the remote repository: git push origin master

Provided nothing else messed up, things should be back to normal now.

4 responses to “How To Fix A Detached Head On A Git Repository”

  1. Mickey says:

    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.”

  2. Antonio says:

    excellent resource!
    Also: what Mickey said.

  3. Jaka says:

    This is not working. I still have detached head!

  4. Siddharth says:

    Between step-3 and 4, we have to switch branch and then delete.

Leave a Reply

Your email address will not be published. Required fields are marked *