Skip to content
Log In Sign Up info@muvcloud.com

What HEAD Is

HEAD is Git's name for "where you are now". Normally it points at a branch, and the branch points at its latest commit. When you commit, the branch moves forward and HEAD moves with it. You can see this in the repository itself:

cat .git/HEAD
ref: refs/heads/main

In a detached HEAD, the file holds a commit hash instead of a branch name. HEAD points straight at a commit, and no branch moves when you commit.

How You Get There

  • Checking out a commit: git checkout a1b2c3d to look at an old version.
  • Checking out a tag: git checkout v1.2.0 to build a release.
  • Checking out a remote-tracking branch: git checkout origin/main instead of main.
  • On purpose: git switch --detach v1.2.0.
  • Tools that do it for you: a rebase in progress, git bisect, and submodules.

The differences between checkout and switch are in git checkout: switch branches and restore files.

It Is a State, Not an Error

Git prints a long notice when you enter a detached HEAD, which makes it look like something broke. Nothing did. You can read files, run tests, build, and even commit. The only catch: commits you make belong to no branch, so once you switch away, nothing points at them. Check where you are with:

git status
git branch

Both say that HEAD is detached and name the commit.

Keep the Work You Did

If you made commits, or have edits you want, create a branch right where you are:

git switch -c fix/from-old-release

HEAD now points at the new branch, and your commits are on it. git checkout -b fix/from-old-release does the same. From there you can merge the branch, push it, or cherry-pick from it like any other.

Leave Without Keeping Anything

git switch -
git switch main

The first line returns to the branch you were on before; the second goes to a branch by name. If you made commits while detached, Git warns that you are leaving them behind and shows their hash, which is your chance to create a branch for them after all:

git branch keep-this a1b2c3d

Uncommitted edits follow the same rules as any switch: Git stops if they would be overwritten. Commit, stash, or discard them first.

Find Commits Made While Detached

If you already switched away and did not note the hash, the reflog still has it:

git reflog

The list is newest first. Look for the commit lines just below the line where you moved to your branch, then create a branch at that hash. Commits that no branch points at are cleaned up by Git's garbage collection once their reflog entries expire, after 30 days by default, so recover them soon.

During a Rebase or a Bisect

A rebase replays your commits on a detached HEAD and moves the branch to the result at the end. If a rebase stops for a conflict, do not create branches or switch away; resolve it and run git rebase --continue, or cancel with git rebase --abort. The steps are in how to resolve a merge conflict in Git.

git bisect checks out one commit after another while it searches for the one that introduced a bug. When you are done, git bisect reset returns you to what you had checked out before git bisect start.

Avoid It When You Do Not Want It

To work on a remote branch, switch to it by its short name, such as git switch feature/api, not origin/feature/api: Git then creates a local tracking branch. To work on an old release, start a branch from the tag directly with git switch -c hotfix/1.2.1 v1.2.0, as shown in how to create a branch in Git.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations