Git: Fix Detached Head

By Xah Lee. Date: .

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 HEAD detached from 2020-03-24 7y9yc
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:

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:

  1. git branch mytemp ddb30c2a97311329cad5645cf715a18e29240746
  2. git checkout master
  3. git merge mytemp
  4. git branch -d mytemp

Here's screenshot of my finally fixed the problem:

fix detached head done 2020-03-24 3qp3x
fix detached head done 2020-03-24 3qp3x

If you have a question, put $5 at patreon and message me.

git