Founding prices until 15 December: 30% off Pro, locked for life. See pricing

$ … You are in 'detached HEAD' state

Git detached HEAD: what it means and how to keep your commits

You checked out a commit or tag, not a branch; new commits belong to no branch.

Git

What it means

HEAD normally points at a branch, which moves with each commit. Checking out a tag or commit points HEAD straight at that commit. You can commit, but when you switch away, nothing points at those commits and they're easy to lose.

Causes and fixes

Most likely first. Run the check, and if it matches, apply the fix.

1. Checked out a tag, a commit or a remote branch directly

Check:

git status; git log --oneline -3

Fix: To keep work done here, git switch -c <new-branch> before leaving.

2. You already switched away and the commits seem gone

Check:

git reflog | head -20

Fix: Find the commit in the reflog and git branch rescue <sha>.

Fix it yourself

A live machine in your browser with this problem already on it. The grader checks the result.

Open the lab

No account needed for your first tries.