Bird
Raised Fist0
Gitdevops~5 mins

Detached HEAD state in Git - Commands & Configuration

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
Introduction
Sometimes in Git, you might check out a specific commit instead of a branch. This puts you in a detached HEAD state, where changes are not linked to any branch. It helps to explore or test old versions without affecting your main work.
When you want to look at the code as it was at a specific past commit without changing branches
When you want to test or build a previous version of your project temporarily
When you want to create a quick fix or experiment without affecting any branch
When you want to inspect or debug a commit before deciding to create a branch from it
When you accidentally checked out a commit and want to understand what happened
Commands
This command switches your working directory to the commit with hash 1a2b3c4d. It puts you in detached HEAD state because you are not on any branch.
Terminal
git checkout 1a2b3c4d
Expected OutputExpected
Note: switching to '1a2b3c4d'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches. HEAD is now at 1a2b3c4d Commit message here
This shows your current state. It will tell you that you are in detached HEAD state and show any changes you made.
Terminal
git status
Expected OutputExpected
HEAD detached at 1a2b3c4d nothing to commit, working tree clean
This command moves you back to the main branch, exiting detached HEAD state and returning to normal branch work.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main' Your branch is up to date with 'origin/main'.
If you want to keep changes made in detached HEAD state, create a new branch from there with this command.
Terminal
git switch -c new-branch
Expected OutputExpected
Switched to a new branch 'new-branch'
-c - Creates and switches to a new branch
Key Concept

If you remember nothing else, remember: Detached HEAD means you are not on a branch, so commits made here can be lost unless you create a branch.

Common Mistakes
Making commits in detached HEAD state and then switching branches without saving them
Those commits become unreachable and can be lost because they are not on any branch
Create a new branch from detached HEAD before switching to save your commits
Confusing detached HEAD with being on a branch
You might think your changes are safe on a branch when they are not linked to any branch
Always check 'git status' to confirm if you are on a branch or detached HEAD
Summary
Use 'git checkout <commit-hash>' to enter detached HEAD state and view a specific commit.
'git status' shows you are in detached HEAD state and your current changes.
Use 'git checkout <branch>' to exit detached HEAD and return to a branch.
Create a new branch with 'git switch -c <branch-name>' to save commits made in detached 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