Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git detached HEAD even exist?

Tags:

git

Our team recently moved to git, migrating from svn and hg. So when they checkout the tip of a tree, depending on the GUIs, they had a tendency to checkout a particular commit or local branch rather than a new tracking branch, without the understanding of how git really works.

So my question is why does detached HEAD even exist?
Why can't it be defaulted to always checking out a new branch? (with my limited understanding of git)

Education/Training definitely helped but there is always someone new to git...
How do you guys manage it?

By the way, I know how to fix them. I've read through a lot of posts on this site.
This is more for knowledge sharing and how you prevent or manage.

Update:
After reading the detailed explanations provided below, I now realized that my question should have been "Why does git give a detached HEAD when checking out a new remote branch?". Regardless, the answer will give you a very good understanding!

like image 283
Steven Xu Avatar asked May 23 '26 09:05

Steven Xu


1 Answers

[...] why does detached HEAD even exist? Why can't it be defaulted to always checking out a new branch?

Let me try with the following metaphor. If you think of your Git repository as a photo album that chronicles the history of your repository...

  • You can think of branches as bookmarks; they mark points of interest in your history, snapshots you're likely to go back to, at some stage... if only out of nostalgia :)
  • You can think of the HEAD reference as one of your fingers, keeping the book open at a particular page.

Now, imagine if you were allowed to open the book only where there is already a bookmark. That would be very restrictive and unwieldy: you would have to create and use many bookmarks just in order to visit certain pages of your history:

enter image description here

Instead, Git allows you to flick through the book and open it on any page you fancy. Then, if you notice a particular snapshot that you've taken interest in, you can always create a new bookmark (branch) for it.

In a nutshell, that's why detached-HEAD state is useful. It allows you to check out any commit, even one which no branch currently points to. If you decide that you'd like to base brand new work on the commit in question, then it would make sense to create a new branch that points to that commit; but, otherwise, creating a new branch would be overkill.


Why does git give a detached HEAD when checking out a new remote branch?

I'm guessing you probably run

git checkout <remote-branch>

and are suprised that it detaches the HEAD. You need to be aware that Git distinguishes between

  • remote-tracking branches, which are local to your repository, but are only meant to reflect what a branch living in a remote repository looked like the last time you communicated with the server; you cannot work on such branches; and
  • (purely) local branches, which can work on.

If you simply run

git checkout <remote-branch>

no local branch for you to play with will be created, and you will end up in detached-HEAD state. You may want to run

git checkout -b <new-local-branch> <remote-branch>

instead. That will create and check out a new local branch pointing at the same commit as the remote branch. No detached HEAD then.

like image 146
jub0bs Avatar answered May 25 '26 01:05

jub0bs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!