HEAD and Detached HEAD

Updated · 12 min read
mediumGit 2.53githeaddetached-headinterview

In a museum, the "You are here" dot on the floor plan usually sits inside a named gallery: "you are in the Painting Gallery". Walk on with the guided tour, and the dot moves with the gallery's route. But you can also step off the route into a small side room that has no name on the map. You can look around there as much as you like. If you leave something in that room, though, nobody will find it again unless you put up a sign.

HEAD is Git's "you are here" dot. Normally it sits on a branch, and when you commit, the branch moves forward and HEAD with it. When HEAD points straight at a commit instead of a branch, Git says you are in detached HEAD state: the side room. It is not an error, and it is safe for looking around. But commits you make there belong to no branch, so you have to give them a name if you want to keep them.

In this lesson, you will see what HEAD contains, go into detached HEAD on purpose, and learn to keep, or get back, the work you do there.

The Starting Point

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git log --oneline
Output
4c2a088 (HEAD -> main) Add the late fee
4009644 Add the term fees
dd34d8a Add project README

What HEAD Contains

HEAD is a small file in .git. Normally it contains the name of a branch:

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

ref: refs/heads/main means "I am on the branch main". This is called an attached HEAD. To find the current commit, Git reads HEAD, follows it to main, and reads the commit ID that main holds. When you commit, Git moves main to the new commit, and HEAD follows automatically, because it points to main.

Go into Detached HEAD

Navika wants to see the project as it was before the late fee. git switch refuses a plain commit ID:

Terminal
git switch 4009644
Output
fatal: a branch is expected, got commit '4009644'
hint: If you want to detach HEAD at the commit, try again with the --detach option.

git switch is for branches. To go to a commit, you have to say that you mean it, with --detach:

Terminal
git switch --detach 4009644
Output
HEAD is now at 4009644 Add the term fees

Git confirms where HEAD is now. Look at HEAD and at fees.txt:

Terminal
cat .git/HEAD
cat fees.txt
Output
400964482ce7eb2e8b5a46144d75f3110aed050a
Term 1 fee: $12,000

Now .git/HEAD holds a commit ID instead of a branch name, and the files are as they were in "Add the term fees": no late fee line. Git shows the state everywhere:

Terminal
git status
git branch
Output
HEAD detached at 4009644
nothing to commit, working tree clean
* (HEAD detached at 4009644)
  main

Look Around, Then Go Back

Detached HEAD is the right way to look at an old version: build it, run it, run its tests, compare it. As long as you don't commit, nothing can be lost. Switching to a branch reattaches HEAD:

Terminal
git switch main
Output
Previous HEAD position was 4009644 Add the term fees
Switched to branch 'main'

Git reminds you where you were (Previous HEAD position was ...) and puts you back on main.

You will meet detached HEAD in a few other places too, all of them normal:

  • Switching to a tag, such as git switch --detach v1.0, because a tag is not a branch.
  • git checkout <commit>, the older command, which detaches without asking for --detach.
  • In the middle of a rebase or a git bisect search, where Git moves HEAD from commit to commit for you.
  • In many CI pipelines, which check out the exact commit to build.

The Older Command Prints a Long Note

git checkout with a commit detaches straight away, and explains the situation in a long note, which you have probably seen before:

Terminal
git checkout 4009644
Output
Note: switching to '4009644'.
 
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.
 
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>
 
Or undo this operation with:
 
  git switch -
 
Turn off this advice by setting config variable advice.detachedHead to false
 
HEAD is now at 4009644 Add the term fees

Every suggestion in the note is covered below. advice.detachedHead is a setting that turns the note off once you know it.

Commits in Detached HEAD

Navika is still detached at "Add the term fees". She tries out a lower late fee and commits it:

Terminal
echo 'Late fee: $200' >> fees.txt
git commit --quiet -am "Try a lower late fee"
git log --oneline --graph --all
Output
* 51931d2 (HEAD) Try a lower late fee
| * 4c2a088 (main) Add the late fee
|/
* 4009644 Add the term fees
* dd34d8a Add project README

The commit worked. But look at the graph: the new commit has HEAD and no branch name. Only HEAD knows about it. Now she switches back to main:

Terminal
git switch main
Output
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
 
  51931d2 Try a lower late fee
 
If you want to keep it by creating a new branch, this may be a good time
to do so with:
 
 git branch <new-branch-name> 51931d2
 
Switched to branch 'main'

Git warns that a commit is being left behind: it is not on any branch, so once HEAD moves away, nothing points to it. git log --all won't show it anymore. Git even prints the command to keep it.

Keep the Work

There are two ways to keep commits made in detached HEAD.

Before you leave, while still detached: git switch -c <name> creates a branch at the current commit and attaches HEAD to it. Nothing is left behind.

After you left, as Navika did: create a branch at the commit. She could copy the ID from Git's warning, but HEAD@{1}, "where HEAD was before the last move", points to the same commit:

Terminal
git branch lower-late-fee HEAD@{1}
git log --oneline --graph --all
Output
* 51931d2 (lower-late-fee) Try a lower late fee
| * 4c2a088 (HEAD -> main) Add the late fee
|/
* 4009644 Add the term fees
* dd34d8a Add project README

The commit now has a name, lower-late-fee, and appears with --all again. It is an ordinary branch that can be switched to, merged or deleted. If more time has passed, git reflog lists every earlier position of HEAD, and you can pick the commit from there.

Attached HEAD points to a branch, detached HEAD points to a commit

Syntax

CommandIt is used to
git switch --detach <commit>Look at a commit in detached HEAD
git checkout <commit>Older form, detaches with a long note
git switch <branch>Leave detached HEAD by reattaching to a branch
git switch -c <name>Keep work done while detached, by creating a branch here
git branch <name> <commit>Keep a commit after leaving it, by naming it
git reflogFind a commit you left behind

Common Mistakes

Panicking about the word "detached". It is a normal state for looking at history. Switch to a branch to leave it.

Working for hours in detached HEAD. Every commit is on no branch. Make a branch with git switch -c as soon as you start real work.

Ignoring the "leaving commits behind" warning. It is the last easy moment to keep them. Copy the command Git prints.

Thinking switching to a tag puts you on a branch. A tag is a fixed name for a commit, so HEAD is detached. Create a branch from the tag to make changes: git switch -c fix/v1 v1.0.

Using git switch <commit> and expecting it to work like git checkout. git switch asks for --detach on purpose, so you never detach by accident.

Under the Hood

Git has two kinds of references. A symbolic reference, like an attached HEAD, contains the name of another reference: ref: refs/heads/main. A direct reference, like every branch and like a detached HEAD, contains a commit ID. git symbolic-ref HEAD asks which branch HEAD points to, and fails when it is detached:

Terminal
git symbolic-ref HEAD
git switch --quiet --detach HEAD~1
git symbolic-ref HEAD
git switch --quiet main
Output
refs/heads/main
fatal: ref HEAD is not a symbolic ref

fatal: ref HEAD is not a symbolic ref is how a script can tell that HEAD is detached. When you commit, Git updates whatever HEAD resolves to: the branch file if HEAD is symbolic, or HEAD itself if it is detached. That is the whole difference.

A commit that no reference and no reflog entry reaches is unreachable, and Git's housekeeping (git gc) eventually deletes it. Commits left behind in detached HEAD are unreachable from branches, which is why the reflog, and its time limit, matters.

Interview Corner

What is HEAD in Git?

A reference to the current commit. Normally it is a symbolic reference to the current branch (ref: refs/heads/main), so commits move that branch forward. It is stored in .git/HEAD.

What is detached HEAD?

A state where HEAD points directly to a commit instead of to a branch. It happens when you check out a commit or tag, during a rebase or bisect, and in many CI checkouts. You can look around and even commit, but new commits belong to no branch.

How do you save commits made in detached HEAD?

Before leaving: git switch -c <name> (or git checkout -b <name>) to create a branch at the current commit. After leaving: find the commit in Git's warning or in git reflog, then git branch <name> <commit>.

How do you get out of detached HEAD?

Switch to a branch: git switch main, or git switch - to go back to the previous one. If you committed while detached, create a branch for that work first.

Why does git switch <commit> fail when git checkout <commit> works?

git switch is designed only for branches, so detaching requires an explicit --detach. That prevents entering detached HEAD by accident.

Practice

Challenge: where am I? In the same repository, go into detached HEAD at main~2 (the first commit), then print the branch list, the first line of git status, and the files in the folder. Then go back with git switch -.

What does Git print? Before you open the solution, decide what git branch shows instead of a branch name, and which files the first commit had.

Show solution
Terminal
git switch --quiet --detach main~2
git branch
git status | head -1
ls
Output
* (HEAD detached at dd34d8a)
  lower-late-fee
  main
HEAD detached at dd34d8a
README.md

git branch lists a line (HEAD detached at dd34d8a) with the *, because HEAD isn't on any branch. The first commit had only README.md, so that is the only file. head -1 prints only the first line of git status. Now go back:

Terminal
git switch -
Output
Previous HEAD position was dd34d8a Add project README
Switched to branch 'main'

git switch - goes back to the previous branch, main, and Git reports the commit it left. Nothing was committed, so nothing was left behind.

Checkpoint. In a practice repository, detach at an older commit, make a commit, and switch back to main. Read Git's warning, then get the commit back with git reflog and git branch.

Quick check: Detached HEAD

4 questions
1What does .git/HEAD contain when you are on main?
2What is detached HEAD?
3You commit in detached HEAD and then switch to main. What happens to the commit?
4Which command keeps your detached-HEAD work by creating a branch right where you are?

Key Takeaways

Quick reference

CommandIt is used to
cat .git/HEADSee whether HEAD names a branch or a commit
git switch --detach <commit>Look at a commit in detached HEAD
git switch <branch>Reattach HEAD to a branch
git switch -c <name>Turn detached work into a branch
git branch <name> HEAD@{1}Name a commit you just left behind
git reflogFind commits left behind
git symbolic-ref HEADPrint the current branch, or fail if detached

Was this tutorial helpful?

Lesson 29 of 106