Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If git was to rename
detached HEAD
I would have it named as a HEAD that isn’t identified by a branch and will soon be forgotten.We as people can easily remember branch names. We do
git checkout new-button-feature
/git checkout main
.main
andnew-button-feature
are easy to remember. And we can just dogit branch
and get a list of all branches. But to do the same with just commits you’d have to dogit reflog
which is very tedious. Because you have thousands of commits but only very few branches.A detached commit’s identifier is just its SHA. So suppose you checked out a commit (not a branch) i.e. you did
git checkout d747dd10e450871928a56c9cb7c6577cf61fdf31
you’ll get:Then if you made some changes and made a commit, you’re still NOT on a branch.
Do you think you’d remember the commit SHA? You won’t!
git doesn’t want this to happen. Hence it’s informing your HEAD is not associated to a branch so you’re more inclined to checkout a new branch. As a result below that message it also says:
To go a bit deeper a branch is built in a way that it’s smart. It will update its HEAD as you make commits. Tags on the other hand are not meant to be like that. If you checkout a tag, then you’re again on a detached HEAD. The main reason is that if you make a new commit from that tag then given that that commit is not referenced by anything (not any branch or tag) then still its considered a detached HEAD.
Attached HEADs can only happen when you’re on a branch.
To look at from another angle, if you’re on a branch and do
cat .git/HEAD
you’d get:Then if you do
cat refs/heads/Your-current-branch-name
then you’d also see the SHA of the commit that your branch is pointing/referencing to.However if you were on a detached HEAD you and
cat .git/HEAD
you’d just get the SHA of the commit and nothing more:By nothing more I mean the head isn’t pointing to any branch. It’s just directly pointing to a commit.
As a result of all this, anytime you checkout a commit (without using the branch name to checkout), even if that commit was the latest commit of your main branch, you’re still in a detached HEAD because your HEAD is not pointing to any of your local branches. Hence even checking out a tag will put you in a detached HEAD. To add onto that, even checking out a remote branch that you have fetched into your computer would result in a detached head ie
git checkout origin main
would also end up as a detached head…Summary
All of the following will cause detached head:
You’re only on an attached head, if you’ve checked out a local branch
If you are working in your repo and do git checkout <SHA> you will be in a “detached HEAD”. You are not on a branch (the commit is likely to be on multiple branches). You are checked out to a specific instance in the history. A detached head can also occur when you are rebasing. You are checked out to a specific commit. You would need to create a branch in order to commit/push changes because you would be creating commits that would be “in limbo” with no way to identify them other than the SHA. Git will remove the commit during its garbage collection because of it not being on a branch. Checkout the “Detached Head” Section on this page for more in depth information: http://git-scm.com/docs/git-checkouthttp://git-scm.com/docs/git-checkout