BEGINNER
1 min DevOps

Git Detached HEAD State

Scenario

A junior developer reports that they checked out a specific commit hash to test a bug, made some fixes, committed them, but now they can’t see their changes on main or any other branch. They are worried they lost their work.

Question

What happened here (technically), and how would you guide them to save their changes and mistakenly “lost” commits to a proper branch?

Expected Answer

  1. Analysis: Explain that detached HEAD means HEAD points directly to a commit, not a branch reference. Commits made here are not reachable by any branch tip.
  2. Solution:
    • Run git branch temp-fix (creates a branch pointer at the current detached commit).
    • Run git checkout main (switches back to main).
    • Run git merge temp-fix (bring changes into main) or create a PR from temp-fix.
  3. Prevention: Advise to always checkout a new branch (git checkout -b <name>) before making changes from a specific commit.