Git: Fix Detached Head
Discovered i have a detached head. Then found it's from last year. Spent 1 hour to carefully fix the incomprehensable git faak.
Explanation:
when you git status
, you get this:

git status
showing git HEAD detached message
HEAD detached means, you are working from a checkout of some commit, not from a branch.
Find out which commit id (and date) is your detached commit:
$ git log 7453bdb6 commit 7453bdb6c3c7bf879fbe5992d43527cfc0421bf6 (master) Author: Xah Lee <xah@xahlee.org> Date: Tue Jun 11 22:19:09 2019 -0700
This is helpful. It tells you which date you started working on this detached head, and perhaps unaware of it.
Fix Detached Head
You should fix it. You have a choice:
- Make your current as the master branch.
- Or, discard your current. Go back to whatever is in master branch.
To make your current as the master branch, do:
git checkout master
To make your detached head as the master branch, do:
First, checkin and commit
git status
git add .
git commit -m "commit of current detached head"
git log -n 1
to get the current commit id:
$ git log -n 1 commit ddb30c2a97311329cad5645cf715a18e29240746 (HEAD) Author: Xah Lee <xah@xahlee.org> Date: Tue Mar 24 17:05:30 2020 -0700
Now, use that id to make a branch, name it mytemp, then checkout master, then merge mytemp into master, like this:
git branch mytemp ddb30c2a97311329cad5645cf715a18e29240746
git checkout master
git merge mytemp
git branch -d mytemp
Here's screenshot of my finally fixed the problem:
