Git Advanced: Git, 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.


One of the states in which your Git repository can end up is “detached HEAD”.

Sounds scary.

Two words: detached (not attached?) and HEAD.

HEAD is the current commit.

When you create a repository, HEAD points to the first commit. If you do a second commit, HEAD points to the new commit.

If you switch branch, HEAD points to the latest commit in that branch.

That’s what’s normal 90% of the time: HEAD points to the latest commit in the currently selected branch. To Git, that’s what it means HEAD being attached.

Our problem is HEAD being detached.

This happens when you checkout a commit that’s not the latest in the branch, and basically means HEAD is not pointing to a branch, but at a commit.

It’s not a situation that’s too unusual. Perhaps you are debugging an issue and you’re trying to figure out in which commit the issue was introduced, so you check out individual commits (using git checkout <commit>) until you find where the code was working.

In this case, when you’re done, you can checkout a branch, for example main, using git checkout main

One thing you can do however is end up in this situation: you are in a detached HEAD state (checked out a commit) and you create a new commit. One or more.

In this case, you need to create a branch before you switch to another branch, otherwise your changes might be lost.

Do so by creating a branch at this commit using:

git branch <new-branch-name>

and then switching to that branch (important):

git checkout <new-branch-name>

Or, with a single command:

git checkout -b <new-branch-name>

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