Normally, `HEAD` (your current location) points to a branch name, like `main`. The branch then points to the latest commit. They move together.
a1b2
←
c3d4
main
HEAD
Key Point: `HEAD` → `main` → `c3d4`. This is the standard, "attached" state. When you make a new commit, both `HEAD` and `main` move forward together.
2
Detaching HEAD
When you check out a specific commit hash (`git checkout a1b2`), `HEAD` detaches from the branch and points directly to that commit.
a1b2
HEAD
←
c3d4
main
Key Point: Now, `HEAD` → `a1b2` directly. The `main` branch is still back at `c3d4`. You are now in a "detached HEAD" state. You're looking at the past, but you're not on any branch.
3
The Danger: Orphaned Commits
If you make a new commit now, only `HEAD` moves forward. The new commit (`e5f6`) has no branch pointing to it. It's an orphan.
a1b2
←
c3d4
main
↑
e5f6
HEAD
Key Point: The new commit `e5f6` is only referenced by `HEAD`. If you switch back to `main`, there is no easy way to get back to this new commit. It will eventually be deleted by Git's garbage collection.
4
The Fix: Create a Branch
To save your orphaned commits, you must give them a name by creating a new branch right where you are. This re-attaches `HEAD` and saves your work.
a1b2
←
c3d4
main
↑
e5f6
new-feature
HEAD
Key Point: Before switching away, run `git checkout -b new-feature`. This creates a new branch `new-feature` pointing to your new commit (`e5f6`) and re-attaches `HEAD` to it. Your work is now safe.