Skip to main content
mentioned that detached had can be caused by checking a remote branch as well
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305

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:

  • checkout any commit
  • checkout any tag
  • checkout any remote branch

You're only on an attached head, if you've checked out a local branch

As a result of all this, anytime you checkout a commit, 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 branches. Hence even checking out a tag will put you in a detached HEAD

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:

  • checkout any commit
  • checkout any tag
  • checkout any remote branch

You're only on an attached head, if you've checked out a local branch

Improved wording
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305

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.

As a result of all this, anytime you checkout a commit, 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 branches. Hence even checking out a tag is will put you in a detached HEAD

Then if you do cat refs/heads/Your-current-branch-name then you'd also see the commit that your branch is pointing/referencing to.

As a result of all this, anytime you checkout a commit, 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 branches. Hence even checking out a tag is will put you in a detached HEAD

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.

As a result of all this, anytime you checkout a commit, 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 branches. Hence even checking out a tag will put you in a detached HEAD

added explanation on what's inside the heads
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
ref: refs/heads/Your-current-branch-name

Then if you do cat refs/heads/Your-current-branch-name then you'd also see 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.

ref: refs/heads/Your-branch-name

However if you were on a detached HEAD you and cat .git/HEAD you'd just get the SHA and nothing more:

ref: refs/heads/Your-current-branch-name

Then if you do cat refs/heads/Your-current-branch-name then you'd also see 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.

deleted 1 character in body
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
Improved wording
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
added another way to inspect and dig deeper
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
added 85 characters in body
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
added attribution
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
improved format
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
added 318 characters in body
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading
Source Link
mfaani
  • 35.5k
  • 19
  • 180
  • 305
Loading