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:
- Debugging: You’re trying to find which commit introduced a bug, so you checkout specific commits to test them
- Exploring history: You want to see how the code looked at a specific point in time
- 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
| State | HEAD Points To |
|---|---|
| Attached | A branch (which points to a commit) |
| Detached | Directly 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-branchis your friend for saving work