Git - Detached HEAD

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





Discover More
Git - Head (Head Branch | Branch Head)

The head is the last commit of a branch. while the HEAD (in uppercase) is the head of the current branch (active branch). headHEADcurrent branch page In the example below, the branch head of: ...
Git - Submodule (modules)

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. package manager A submodule is materialzied as: a subdirectory in your working directory. with its metadata...
Branches Git
Git - Branch

This page talks Branch management in Git. For Git, a branch is: a commit name (ref) that points to the last commit (head) leaf in the commit log (or log) that represents a commit chain Example...



Share this page:
Follow us:
Task Runner