Table of Contents

About

Being in detached head means that the head value is:

Consequences:

  • You are therefore no longer on a working branch
  • You have checked out to a single commit

Commands that set the head in a detached value

You will be set in a detached mode when you don't checkout a local branch and you will see the following text

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

For instance:

  • a specific commit
# ie parent commit of main
git checkout main^
# grandparent of current HEAD
git checkout HEAD~2         
git checkout remote/branchName
# for instance
git checkout origin/main
  • A tag (because you can't add commit to a tag
git checkout tagname        

How to verify that you are in a detached head

The head value is:

ref: refs/heads/branch.

How to create a branch that retain commit

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Turn off this advice by setting config variable advice.detachedHead to false

Documentation / Reference