Skip to content

Lesson

HEAD

What HEAD points at, how checkout and switch move it, and what detached HEAD means.

Intermediate 5 min read

Learning objectives

  • Explain what HEAD references
  • Distinguish attached from detached HEAD
  • Predict where new commits land

HEAD follows a branch

Normally HEAD is a symbolic reference to a branch, like refs/heads/main. 'Where am I?' is answered by HEAD. When you commit, HEAD's branch moves and HEAD moves with it.

Detached HEAD

Checking out a commit or tag directly detaches HEAD. You can look around and even commit, but those commits are not on any branch. Create a branch before committing if you want to keep the work.

Try it yourself

interactive visualization

Empty folder

Run git init to create a repository.

Working directory

Clean — no uncommitted changes.

git add ↓

Staging area

Nothing staged. Run git add.

git commit ↓

Repository

No commits yet.

1/4Empty repositoryOpen in visualizer

Observe HEAD as it points at your branch and follows commits.

Commands used

terminal
git log --oneline
git checkout HEAD
git switch main

Common mistakes

  • Treating detached HEAD as an error rather than a state.
  • Losing commits made while detached.

Best practices

  • Create a branch before experimenting from an old commit.

Mini challenge

Explain, in one sentence, where HEAD points after git switch -c test.

Open the visualizer to try it

Summary

  • HEAD marks your current position.
  • It usually points at a branch, which points at a commit.
Next lessonMerge