THE GITHTML FIELD GUIDE
Save commits made in detached HEAD state
Detached HEAD means HEAD points directly to a commit rather than through a local branch. If you made commits there, create a new branch at the current commit before switching away. That gives the work a durable, discoverable name. If you already left, inspect the reflog and verify the old tip before creating a recovery branch.
Decide whether detached state is a problem
Checking out a tag or historical commit can intentionally detach HEAD for inspection. Nothing is wrong if you only read files and then return to your branch. The risk appears when you create valuable new commits and leave without a reference keeping them easy to find. Detached does not mean the commits failed; it means no ordinary local branch advanced with them.
Check status for uncommitted changes as well. Creating a branch at HEAD protects committed history but does not snapshot unsaved editor buffers or untracked files. Preserve those separately when necessary. Avoid running reset or checkout force to remove the detached warning, because that addresses the display at the expense of the actual work.
Name the current history before leaving
Choose a new branch name that does not already exist. The switch -c command creates that branch at the current commit and checks it out without replaying the commits. Inspect the log afterward to confirm the tip contains your work. This is local preservation; it does not publish anything.
- If you already switched away, use git reflog --date=iso to locate the detached tip, inspect it with git show, and create a new branch at the verified identifier.
- Do not assume the most recent reflog entry is the lost commit. Switching, resets, and other operations can add entries after the work you need.
git status --short
git log --oneline -5
git switch -c recovery/detached-work
git branch --show-current
git log -1 --onelineIntegrate only after the work is safe
Once the recovery branch exists, decide whether to continue there, open a contribution from it, or apply selected commits to another branch. That integration decision is separate from preserving the detached history. Review dependencies and the intended base before cherry-picking or merging.
Reflogs are local and expire; they are not an indefinite backup service. Create a named reference promptly when you find important work and verify it before any cleanup. If the work includes secrets, keep the recovery branch local while following the appropriate incident process. The success criterion is that the exact useful commits are reachable through a clear branch name and any remaining uncommitted content is still accounted for.
- When a detached session contains several new commits, name its final tip rather than only the first useful commit. Inspect the resulting branch’s log to ensure the earlier work is included through ancestry. A branch created at an earlier point would leave later detached commits outside that branch.
- If the detached work began at an old release tag, compare that starting point with the branch where you intend to integrate it. A recovery branch preserves the old baseline too. Test any later cherry-pick against current interfaces instead of assuming successful preservation also establishes a suitable integration base.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.