How to fix Detached HEAD state in git?
  |   Source

Sometimes we have to check out the old versions without creating a new branch. In this situation HEAD becomes detached because it point no where. Once we are in this situation, there are multiple outcomes possible:

  1. Discard the changes and return to master
  2. Merge changes between master and detached head.
  3. Retain the changes by forcing master branch to attach to detached head.

This blog post covers the third scenario. If you want to retain the changes and reset master branch, then only follow these steps: