How To Fix A Detached Head On A Git Repository

Last updated: May 1, 2014

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.

 

Dwayne

 

2 thoughts on “How To Fix A Detached Head On A Git Repository

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

Leave a Reply

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