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!
[...] 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...
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:

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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With