Level 15

Detached HEAD

Point HEAD at a commit with no branch, understand why it is useful, and get back out safely.

You are on Level 15, the second-to-last level. In this one you learn about an often confusing Git concept with a morbid name: the detached HEAD state. You will find out what it is, when it occurs, why it is useful, and how to get out of it. Rest assured, at no point will your own head become detached.

Step 1 of 6
0 xp

A moveable bookmark

As a keen observer, you probably noticed something in every scenario so far: the blue HEAD has always sat on top of a green marker. In scrapbook terms, you have only ever opened the book to the first page of a section.

HEAD is a moveable bookmark that always marks the page your scrapbook is currently open to, which is to say, which commit is currently checked out. That is the whole reason it exists.

Three ways HEAD has moved

  1. HEAD moves forward when you insert a new page, a commit. Git advances the section marker to the new page and HEAD along with it. Both move together, so HEAD stays attached to the branch.
  2. HEAD moves when you switch branches. That is flipping the book from one section marker to another. Still attached.
  3. HEAD moves when you reset. Both HEAD and the branch it points to move to the specified page. Still attached.

If HEAD could only ever sit on a page with a marker, you could only ever open the book to those pages. You would never be able to look at the mementos on any other page, or at the code in any commit without a green marker on it.

The --detach flag

What if you want to see what your files looked like at the time of the second commit in the chain? It has no marker, so how can you switch to it? git switch has a flag for exactly this:

git switch <commit> --detach

Instead of a branch name you give any old commit, and --detach tells Git it is OK to enter a detached HEAD state: HEAD pointing at a commit that has no branch. It is like opening the scrapbook to a page without a section marker.

Open an older page

Open the page one behind HEAD, the one with no marker:

Type this into the terminal below
git switch --detach HEAD^

Notice how HEAD moved back one commit from where it was, and now sits on a commit with no green marker on it. You are officially in a detached HEAD state. (Does it hurt?)

The files on your table now reflect the commit HEAD points to: feature.html is gone from the table, because that page does not have it. This is useful because it lets you examine the code at any point in your history, not only at the points marked by branches.

safe Switches to 'HEAD^'.

Detached

Here is what detached looks like in the book: the bookmark is clipped straight onto a page, with no marker under it.

There is a reason to be careful here. If you change code while detached and commit it, Git will faithfully create the page. But there is no branch marker to move forward onto it. Switch back to an existing branch and you could lose track of that page: not deleted, but hard to find again, since no marker leads to it. To avoid that, plant a marker first: git switch -c <name> creates a branch right here and attaches HEAD to it.

Your turn: reattach

You examined the code at this page and it looks good. Which command gets you out of the detached state and back to your latest work?

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.