Git detached HEAD: keep your work on a branch
- Published: 2026.11.03
- Updated: 2026.11.03
- Development
You check out an earlier commit to inspect a bug, then Git reports a detached HEAD. Your files are still there, but new commits will not advance the branch you were using. Before switching away, decide whether this checkout was only for inspection or whether it now contains work you want to keep.
In a normal checkout, HEAD points to a branch name. In a detached checkout, it points directly to a commit. A detached HEAD is a valid Git state; the important part is giving useful work a branch before you leave it.
Check the current state
Run these commands inside the repository:
git status --short --branch
git branch --show-current
The status header can indicate a detached checkout. The second command prints the current branch name when you are on a branch; it prints no branch name when HEAD is detached. Also read the file entries in status: staged changes, unstaged changes, and untracked files are separate from commits.
git log -3 --oneline
The log shows recent commits reachable from the current checkout. Inspect the messages before deciding that there is nothing to preserve. Detached commits are real commits, even though your original branch has not moved to include them.
Keep work on a new branch
If you are still at the detached commit you want to keep, create and switch to a new branch at that position:
git switch -c keep-detached-work
git branch --show-current
git log -1 --oneline
Use a new name that describes your task. The branch should now point to the same commit that HEAD pointed to before the command. Creating that name does not merge the work into main and does not publish it to a remote.
Uncommitted changes remain uncommitted. Check status and review your changes before making any commit. If the branch name already exists, choose a different name. The uppercase -C option can reset an existing branch, so it is not a substitute for choosing a free name.
Return after inspection only
If you only inspected an old commit, made no new commits, and have no file changes to keep, switch back to the branch you intended to use:
git switch main
git status --short --branch
Replace main with the actual branch name in your repository. Git normally refuses a switch that would overwrite local changes. Read that refusal and preserve the affected work rather than adding a force option. If you made detached commits, create the preserving branch first.
If you already switched away
git reflog -5
The local reflog can help identify commits HEAD visited. Read the entries and inspect the intended commit before creating a branch at it. Reflog history is local and expires; it is not a remote backup or a guarantee that every old commit remains available. The Git detached HEAD documentation explains how references preserve those commits.
If your immediate problem is staged files rather than a detached checkout, unstaging files while keeping edits addresses a different part of Git’s state. For unexpected whitespace in a patch, checking staged and unstaged whitespace errors helps before the next commit.
References
-
Previous article
Check which command Bash will run with command -v and type -a 2026.10.25
-
Next article
Inspect a Git stash without applying it: list, files and patches 2026.11.08