0
0
Gitdevops~10 mins

Detached HEAD state in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Detached HEAD state
Start on branch
Checkout commit or tag
HEAD points directly to commit
Detached HEAD state active
Make commits?
Commits not on branch
Switch back to branch to reattach HEAD
This flow shows how checking out a commit or tag moves HEAD away from a branch, creating a detached HEAD state where commits are not on any branch.
Execution Sample
Git
git checkout 1a2b3c4d
# Detached HEAD at commit 1a2b3c4d

git commit -m "temp change"
# Commit made but not on any branch

git checkout main
# HEAD reattached to branch main
This sequence shows checking out a commit causing detached HEAD, making a commit, then returning to a branch to reattach HEAD.
Process Table
StepCommandHEAD points toBranchNotes
1git checkout maincommit hash of main tipmainHEAD attached to branch main
2git checkout 1a2b3c4dcommit 1a2b3c4dnoneDetached HEAD state: HEAD points directly to commit
3git commit -m "temp change"new commit hashnoneCommit created but not on any branch
4git checkout maincommit hash of main tipmainHEAD reattached to branch main
💡 HEAD reattached to branch main, detached HEAD state ends
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4
HEADpoints to main branch tippoints to commit 1a2b3c4d (detached)points to new commit (detached)points to main branch tip (reattached)
Branch mainpoints to commit hash Apoints to commit hash Apoints to commit hash Apoints to commit hash A
Key Moments - 3 Insights
Why does HEAD not point to any branch after 'git checkout 1a2b3c4d'?
Because you checked out a specific commit, not a branch name, so HEAD points directly to that commit, creating a detached HEAD state as shown in execution_table row 2.
What happens to commits made in detached HEAD state?
They are not on any branch and can be lost if you switch branches without saving them, as seen in execution_table row 3 where commit is made but branch remains unchanged.
How do you get out of detached HEAD state?
By checking out a branch again, which reattaches HEAD to that branch, shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, after which step is HEAD detached?
AAfter step 2
BAfter step 3
CAfter step 1
DAfter step 4
💡 Hint
Check the 'HEAD points to' and 'Branch' columns in execution_table row 2
At which step does HEAD reattach to a branch?
AStep 2
BStep 3
CStep 4
DNever
💡 Hint
Look at the 'Branch' column in execution_table row 4
If you make a commit in detached HEAD state and then switch branches without saving, what happens to that commit?
AIt becomes part of the branch history
BIt is lost unless referenced
CIt merges automatically
DIt deletes the branch
💡 Hint
Refer to key_moments answer about commits in detached HEAD state and execution_table row 3
Concept Snapshot
Detached HEAD state occurs when HEAD points directly to a commit, not a branch.
This happens by checking out a commit or tag.
Commits made here are not on any branch and can be lost.
To exit, checkout a branch to reattach HEAD.
Always save work before switching branches from detached HEAD.
Full Transcript
Detached HEAD state in git happens when you checkout a specific commit or tag instead of a branch. This causes HEAD to point directly to that commit, not to any branch. In this state, if you make commits, they are not part of any branch and can be lost if you switch branches without saving. To get out of detached HEAD state, you checkout a branch again, which reattaches HEAD to that branch. The execution table shows these steps clearly: starting on a branch, checking out a commit to detach HEAD, making a commit in detached state, and finally checking out the branch again to reattach HEAD.