Git Advanced: Understanding Detached HEAD

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


“Detached HEAD” sounds scary, but it’s a normal Git state that’s easy to understand and recover from.

What is HEAD?

HEAD is a pointer to the current commit you’re working on.

  • When you create a repository, HEAD points to the first commit
  • After each new commit, HEAD moves to point to that commit
  • When you switch branches, HEAD points to the latest commit on that branch

Normally, HEAD points to a branch, which points to a commit. That’s HEAD being “attached.”

What is Detached HEAD?

Detached HEAD happens when HEAD points directly to a commit instead of a branch.

This occurs when you checkout a specific commit:

git checkout 5a06d3ca5e7adb6e67

Now HEAD points directly at that commit, not at any branch.

Why Would You Enter Detached HEAD?

Common scenarios:

  1. Debugging: You’re trying to find which commit introduced a bug, so you checkout specific commits to test them
  2. Exploring history: You want to see how the code looked at a specific point in time
  3. Using git bisect: The bisect process checks out commits automatically, putting you in detached HEAD state

Getting Back to Normal

When you’re done exploring, simply checkout a branch:

git checkout main

HEAD is now attached to main again.

The Dangerous Scenario

The problem arises when you make commits while in detached HEAD state.

git checkout abc123      # Now in detached HEAD
# make some changes
git add .
git commit -m "Important fix"
# You now have a commit that no branch points to!

If you checkout a different branch now, those commits could be lost (eventually garbage collected) because no branch references them.

Saving Work from Detached HEAD

If you’ve made commits in detached HEAD state, create a branch before switching:

# Create a branch at the current commit
git branch my-rescue-branch

# Switch to it
git checkout my-rescue-branch

Or do both in one command:

git checkout -b my-rescue-branch

Now your commits are safe on a proper branch.

Summary

StateHEAD Points To
AttachedA branch (which points to a commit)
DetachedDirectly to a commit

Key takeaways:

  • Detached HEAD is normal when exploring history
  • Checkout a branch when you’re done looking around
  • If you make commits in detached HEAD, create a branch before leaving
  • git checkout -b new-branch is your friend for saving work

Lessons in this unit:

0: Introduction
1: Working with Remotes
2: Squashing Commits
3: Rebase vs Merge
4: Git Bisect for Debugging
5: Git Worktrees
6: Git Submodules
7: ▶︎ Understanding Detached HEAD
8: Managing Secrets in Git
9: Git Workflows and Best Practices
10: How to push to 2 repositories at the same time and keep them in sync
11: How to update a Git branch from another branch
12: Git, detached HEAD
13: Trigger deploys on Netlify with submodules
14: A Git Cheat Sheet
15: Git, squashing vs not squashing
16: An incomplete list of great Git tutorials
17: Git, what if you forgot to add a file to a commit?
18: Git workflow to manage work on multiple branches
19: How to setup the Git SSH keys