Bird
Raised Fist0
Gitdevops~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does it mean when Git is in a detached HEAD state?
easy
A. You are merging two branches together.
B. You are on the latest commit of the main branch.
C. You are viewing a specific commit, not a branch.
D. You have uncommitted changes in your working directory.

Solution

  1. Step 1: Understand HEAD in Git

    HEAD usually points to the latest commit on a branch, representing your current working state.
  2. Step 2: Meaning of detached HEAD

    When HEAD points directly to a commit instead of a branch, you are in detached HEAD state, meaning you are not on any branch.
  3. Final Answer:

    You are viewing a specific commit, not a branch. -> Option C
  4. Quick Check:

    Detached HEAD = viewing commit, no branch [OK]
Hint: Detached HEAD means no branch, just a commit [OK]
Common Mistakes:
  • Confusing detached HEAD with uncommitted changes
  • Thinking detached HEAD means merging branches
  • Assuming detached HEAD is always on main branch
2. Which Git command puts you into a detached HEAD state?
easy
A. git checkout main
B. git branch new-feature
C. git merge feature-branch
D. git checkout

Solution

  1. Step 1: Understand git checkout usage

    Checking out a branch moves HEAD to that branch's latest commit, staying attached.
  2. Step 2: Checkout a commit hash

    Checking out a specific commit hash moves HEAD directly to that commit, causing detached HEAD state.
  3. Final Answer:

    git checkout <commit-hash> -> Option D
  4. Quick Check:

    Checkout commit hash = detached HEAD [OK]
Hint: Checkout commit hash, not branch, to detach HEAD [OK]
Common Mistakes:
  • Using branch name instead of commit hash
  • Confusing branch creation with checkout
  • Thinking merge causes detached HEAD
3. What will be the output of the following commands?
git checkout 1a2b3c4
git status
Assuming 1a2b3c4 is a valid commit hash.
medium
A. HEAD detached at 1a2b3c4 nothing to commit, working tree clean
B. On branch main Your branch is up to date.
C. error: pathspec '1a2b3c4' did not match any file(s) known to git
D. You are currently rebasing branch 'main'

Solution

  1. Step 1: Checkout commit hash

    Running git checkout 1a2b3c4 moves HEAD to that commit, entering detached HEAD state.
  2. Step 2: Check git status output

    In detached HEAD, git status shows 'HEAD detached at <commit>' and clean working tree if no changes.
  3. Final Answer:

    HEAD detached at 1a2b3c4 nothing to commit, working tree clean -> Option A
  4. Quick Check:

    Detached HEAD status shows commit and clean tree [OK]
Hint: Detached HEAD status shows commit hash and clean tree [OK]
Common Mistakes:
  • Expecting branch name in status
  • Thinking checkout commit hash causes error
  • Confusing rebase message with detached HEAD
4. You are in detached HEAD state and made some changes. Which command will save your changes safely on a new branch?
medium
A. git commit -m 'save changes'
B. git checkout -b new-branch
C. git merge main
D. git reset --hard

Solution

  1. Step 1: Understand detached HEAD changes

    Changes made in detached HEAD are not on any branch and can be lost if you switch commits.
  2. Step 2: Create a new branch to save changes

    Using git checkout -b new-branch creates a branch at current commit and switches to it, preserving changes.
  3. Final Answer:

    git checkout -b new-branch -> Option B
  4. Quick Check:

    Create branch from detached HEAD to save changes [OK]
Hint: Create new branch from detached HEAD to keep changes [OK]
Common Mistakes:
  • Committing without a branch loses changes on checkout
  • Merging without branch context does not save changes
  • Resetting discards changes
5. You checked out a commit hash and made changes in detached HEAD. Later, you want to keep those changes and continue working on a branch named feature. What is the correct sequence of commands?
hard
A. git checkout -b feature git add . git commit -m 'work'
B. git commit -m 'work' git branch feature git checkout feature
C. git add . git commit -m 'work' git checkout feature
D. git checkout feature git merge HEAD

Solution

  1. Step 1: Create and switch to new branch

    Use git checkout -b feature to create a branch at current detached HEAD and switch to it.
  2. Step 2: Stage and commit changes

    Run git add . and git commit -m 'work' to save your changes on the new branch.
  3. Final Answer:

    git checkout -b feature git add . git commit -m 'work' -> Option A
  4. Quick Check:

    Create branch first, then commit changes [OK]
Hint: Create branch first, then add and commit changes [OK]
Common Mistakes:
  • Committing before creating branch loses changes
  • Switching to branch before committing loses detached changes
  • Merging detached HEAD into branch is incorrect