Skip to main content
1 of 3
Keshav
  • 2k
  • 19
  • 22

In simple words, Detached HEAD state means you are not checked out to HEAD (or tip) of any branch.

Understand With Example

A branch in most of the cases is sequence of multiple commits like:-

commit 1: master-->branch_HEAD(123be6a76168aca712aea16076e971c23835f8ca)

commit 2: master-->123be6a76168aca712aea16076e971c23835f8ca-->branch_HEAD(100644a76168aca712aea16076e971c23835f8ca)

As you can see above in case of sequence of commits your branch points to your latest commit. So in that case if you checkout to commit 123be6a76168aca712aea16076e971c23835f8ca then you would be in detached head state since HEAD of your branch points to 100644a76168aca712aea16076e971c23835f8ca and technically you are checked out at HEAD of no branch.Hence, you are in detached HEAD state

Theoretical Explanation##

In this Blog its clearly stating Git repository is a tree-of-commits, with each commit pointing to its ancestor with each commit pointer is updated and these pointers to each branch are stored in the .git/refs sub-directories. Tags are stored in .git/refs/tags and branches are stored in .git/refs/heads. If you look at any of the files, you'll find each tag corresponds to a single file, with a 40-character commit hash and as explained above by @Chris Johnsen and @Yaroslav Nikitenko you can check out these references

Keshav
  • 2k
  • 19
  • 22