Detached HEAD means you checked out a commit instead of a branch — new commits can be lost. Here's how to tell if you need to save work and how to get back to a branch safely.
git checkout <sha>, git checkout v1.2, or git checkout origin/main all move HEAD off a branch. Any commit you make now belongs to no branch and is only reachable by its SHA.
After a fetch, a deleted upstream branch can leave your checkout detached. Check with: git branch --contains HEAD — if nothing lists, you're off-branch.
git status --short && git log --oneline -3 && git branch --contains HEAD
git switch -c rescue-work # then push it: git push -u origin rescue-work
git switch main # or: git switch - to return to the previous branch
git reflog --date=relative | head -20 # find the sha, then: git branch recovered <sha>
git reflog keeps unreachable commits for ~90 days by default, so almost nothing is truly lost immediately. To inspect a commit without detaching, prefer: git switch --detach is the same thing — use git log / git show instead.
No — commits are not deleted, they just stop being referenced. Run git reflog, find the SHA where you made them, and create a branch at it with git branch <name> <sha>.
Push uploads branches, and you're not on one. Your commits exist only in your local object store. Create a branch first, then push it.
Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.
Browse the template store →One-time. Yours to modify. Instant download from the NinjaOps template store.