BXUP Track progress & test yourself — free
War Room · Field diagnosis · ~2 min read
WAR ROOM · FIELD DIAGNOSIS

WR-004 · Detached HEAD — "my commits vanished"

Symptom

You ran git checkout <commit-id> to look at an old version. Git printed a long warning: You are in 'detached HEAD' state.... You kept working, made one or two commits, then switched back with git checkout main — and your new commits are gone. git log does not show them on any branch. Panic sets in: hours of work, apparently deleted.

Context

First month with Git, usually while exploring history: comparing an old version of a script, chasing when a bug appeared, or following a tutorial that checks out a commit id. Also triggered by checking out a tag or a remote branch name without creating a local branch. Linked module: 0.2 (Git & GitHub as an engineer).

Diagnosis

HEAD is Git's pointer to "where you are now". Normally it points at a branch name, and the branch moves forward with each commit. git checkout <commit-id> points HEAD directly at a commit instead — that is all "detached" means. Commits you make there are real and safely stored, but no branch label points at them. When you switch back to main, nothing references them, so no ordinary git log shows them. They are unreachable, not deleted: Git keeps unreachable commits for at least 30 days, and git reflog — the private diary of every position HEAD has visited — still lists them. Confirm your case: run git reflog and look for your commit message in the recent entries with an id beside it.

The fix

First, stop: do not re-type the work, and do not run any cleanup commands. Run git reflog and find the line with your lost commit's message; copy its short id. Pin a branch to it: git branch rescue <id>. Now git log rescue --oneline shows your work at the top. Bring it home with a normal merge (git checkout main, git merge rescue) or keep working on the rescue branch and raise a pull request. If you are still sitting on the detached commit, it is even easier: run git switch -c rescue before going anywhere, and the work is on a branch.

Prevention

Before experimenting with old commits, create a branch first: git switch -c experiment <commit-id> gives you the same view with a safety label attached. Read the terminal's own warning — the detached HEAD message contains the exact git switch -c fix. Make git reflog a known tool before you need it, the way you keep a torch before a power cut. And push your branches daily: a pushed commit survives even a stolen laptop.

In an interview

Q: "A junior tells you Git deleted their commits after they checked out an old version. What happened, and how do you recover?" Strong answer: define detached HEAD in one sentence (HEAD on a commit, not a branch), state that the commits are unreachable rather than deleted, name git reflog as the recovery index and git branch rescue <id> as the pin, and add the 30-day garbage-collection window — that detail signals real recovery experience, not tutorial reading.

This is the free reading view. Open BXUP to track your progress, run the hands-on labs, and prove it with original, exam-pattern assessments.
© Anvi Innovate · BXUP — Be Exponentially Upskilled · BXUP is an independent learning platform, not affiliated with or endorsed by AWS, Microsoft, Google, NVIDIA, IBM, Salesforce, Databricks, Oracle or Anthropic. All trademarks belong to their respective owners. · bxup.in