Git detached HEAD: save a commit and recover it with reflog
Detached HEAD is normal. Learn how to keep a commit on a branch, carry uncommitted edits, and find a commit with Git's reflog.
Seeing “detached HEAD” in Git does not mean your repository is broken. Git has checked out a commit directly instead of switching to a named branch. You can keep editing and committing, but a new commit will not advance a branch name. If you want to keep the work, create a branch before switching away:
git switch -c rescued-work That creates a branch at the commit you are on and switches to it. Your staged and unstaged file changes stay in the working tree, but they are still uncommitted until you review and record them.
This guide uses the Git command line. We checked the two recovery paths in a disposable repository with Git 2.39.5: creating a branch from detached HEAD and rescuing a commit through the reflog. Graphical clients can describe the same state differently, so the commands below are the clearest way to confirm what Git is doing.
What “detached HEAD” means
In an ordinary checkout, HEAD names a branch, and that branch points to a commit. When you make a commit, Git moves the branch to the new commit.
In a detached state, HEAD points directly to a commit instead. Git still lets you edit files and create commits, but those commits advance HEAD without moving a named branch. The Git data model documentation and the detached HEAD section of `git-checkout` describe this relationship.
Detached HEAD is useful when you want to inspect an older commit or try a short experiment. The risk begins when you switch away after creating a commit that no branch or tag points to: it can become unreachable and Git may eventually remove it during cleanup. Creating a branch keeps the commit reachable by a named reference.
If you are still detached, create a branch now
First check where you are and whether you have file changes:
git status If the repository is in the detached state and you want to keep the current history, create a new branch from the commit you are viewing:
git switch -c rescued-work Choose a branch name that does not already exist. The -c option creates a new branch and switches to it; it does not overwrite an existing branch. Avoid substituting -C, which can reset an existing branch to the starting commit. If your Git installation does not recognise git switch, the older equivalent is:
git checkout -b rescued-work Both commands start the new branch at the current commit. If you made one or more commits while detached, the branch now points to the latest one and keeps its earlier commits in the same history. It does not merge that work into main or another branch; it gives you a safe place to decide what to do next.
Check that Git has switched to the new branch:
git status --short --branch
git log -1 --oneline --decorate The status should name rescued-work, and the log line should show HEAD -> rescued-work beside the commit. If you still have staged, unstaged or untracked changes, they should remain visible in git status. Review them before committing; creating a branch does not turn file edits into a commit.
If you want to keep a name for the current commit but continue inspecting it without switching HEAD, use git branch rescued-work. That creates a branch at the current commit and leaves you detached. You can switch to it later with git switch rescued-work.
If you already switched away, look in the reflog
Git keeps a local record of recent reference changes called the reflog. The HEAD reflog also records branch switches, so it can help you find a commit you made while detached. From the repository, run:
git reflog -n 10 Look for a recent commit: entry whose message matches your work. The short hash appears at the start of the line. If it is not among the ten entries, run git reflog without -n 10 and scan further back. Inspect a candidate before keeping it:
git show --stat <commit-id> Replace <commit-id> with the hash from the reflog. If the files and commit message look right, create a branch at that commit:
git branch rescued-work <commit-id>
git log -1 --oneline --decorate rescued-work The first command adds a branch name to the recovered commit without switching your current checkout. The final log check should show rescued-work pointing to the commit you selected. If you want to continue working there, switch only after you have confirmed the branch and checked your current file changes.
The Git reflog manual explains that reflogs record reference changes in the local repository. They are not an online backup and entries can expire, so look promptly. A reflog can help recover commits; it cannot restore uncommitted edits that were overwritten or deleted when you switched branches. For those, check any editor history or separate file backups.
When detached HEAD is fine
If you only checked out a tag or older commit to read the code, and you have no changes or commits to keep, simply switch back to the branch you intended to use:
git switch <branch-name> If you expect to experiment from an older commit, start on a branch instead:
git switch -c experiment <commit-id> The `git-switch` manual documents both creating a branch from the current commit and starting one from a specified commit. A detached checkout is a normal way to inspect history; when you want the experiment to last, give it a branch before moving on.