Git School · FluxGit

Git detached HEAD: preserve your commits

FluxGit ·

These blocks describe different cases, not one script. Choose the case that matches your repository. Replace example branch names, file paths and VERIFIED_COMMIT before use. Abort commands apply only to their active operation; do not run both alternatives.

Git School · G10

Try it in your browser

You checked out an old commit, made a commit there, and Git says "HEAD detached".

Goal: Put your commit on a branch so it cannot get lost.

Practice uses a simulated repository in your browser memory. It does not access your files or connect to a real remote repository.

Commands to practise

This simulator supports a subset of Git commands. Unsupported commands explain their limits in the terminal.

git status
git switch -c new-layout

With FluxGit

  • The toolbar shows "Detached HEAD" instead of a branch name, so you notice before you commit.
  • Right-click the commit and choose Create branch here: the work is safe in one step.
Download FluxGit Git School

Short answer

Detached HEAD means HEAD names a commit directly instead of following a local branch. You can inspect, build and even commit in this state. If you want to keep new work, create a named branch at the current commit before switching away. A detached checkout is not itself data loss; leaving new commits without a durable reference creates the recovery problem.

The example and its prerequisites

This example starts with a temporary checkout of an older release and two new commits to preserve as a fix branch. This is different from an unfinished rebase or a submodule checkout, which can deliberately use detached HEAD. Read status first. If another operation is active, finish or abort that operation deliberately instead of using a detached-state tutorial to interrupt it.

Inspect before changing anything

git branch --show-current is empty in the ordinary detached case. git status explains the state, and git log shows the current committed history. Record HEAD's object ID before doing anything else. An empty branch name does not mean the repository has no branches, nor that its files are untracked. HEAD and the working-tree contents answer different questions.

git status
git branch --show-current
git log -3 --oneline
git rev-parse HEAD

Keep a reference or a separate checkout

Create rescue/detached with git switch -c while still at the commit you want. The new branch points to the current commit and keeps its ancestry reachable. This names committed work; it does not turn unstaged file edits into a commit. Inspect and commit those edits separately if appropriate. Use a fresh branch name rather than replacing an existing branch reference with a force option.

git switch -c rescue/detached
git branch --show-current

The first workflow

After creating the branch, confirm the current branch name, recent commits and patch. You can continue working there or merge/cherry-pick the selected fix into another branch. A cherry-pick copies a change into a new commit; merging joins histories. Choose according to the task and inspect the result. No automatic push is required simply to leave detached HEAD safely.

git log -3 --oneline --decorate
git show --stat HEAD

Choose the other outcome deliberately

If you already switched away, inspect the local reflog and the candidate commit with git show. Choose the object whose patch and parent are correct, then create rescue/recovered at that verified ID. VERIFIED_COMMIT in the example is a placeholder you must replace. Do not assume HEAD@{1} is always the lost commit: every later checkout or operation can change the relevant entry.

git reflog --date=iso
git show VERIFIED_COMMIT
git branch rescue/recovered VERIFIED_COMMIT

Limits and exceptions

Reflog records local reference movements. It is not a remote archive, and entries and unreachable objects can expire or be pruned. Uncommitted edits were never recorded as commits by reflog. If your lost work was only in a file buffer or working tree, look for a copy that existed before the loss, such as an editor backup or a verified stash; do not promise a reflog command will recreate it.

Conflicts and cleanup

A submodule often checks out the parent repository's recorded commit in detached HEAD. That is normal when you only consume the pinned dependency. If you intend to develop the child repository, create its own branch inside that repository and follow its contribution workflow. A branch in the parent does not automatically name commits in the child; their references and histories belong to different repositories.

Verify the result

Before deleting a rescue branch, check that a durable branch or reviewed merge still reaches the wanted commits. Inspect status and the patch on the destination. Keep the rescue reference while the work is under review. A branch name is a handle for committed history, not a second physical backup of all project files or an assurance that the fix passes tests.

git log --oneline --decorate rescue/detached
git status --short

Common questions

Is detached HEAD an error? Not when you intentionally inspect a commit, tag or pinned submodule. Can I commit there? Yes, but name the resulting history before leaving if you want to keep it. Can I recover later? Sometimes, when the local objects and evidence still exist; inspect candidates instead of promising permanent retention.

Review the operation in FluxGit

Use FluxGit’s visual history and patch view to review the operation and the relevant commits. Keep the branch, sharing and local-file checks in this example. The download page lists the currently available builds and platforms.

Official Git references

Git School · 1:37

Detached HEAD in Git: what it is and how not to lose your work

Git says you are in detached HEAD state. You checked out an old tag, made a fix, and committed it. Now what?

Detached HEAD in Git: what it is and how not to lose your work
English narration. Manual subtitles are available on YouTube in 11 languages. This transcript is in English.

Watch on YouTube — opens a new tab

Recorded in a test repository.

Read the narration transcript
  1. 00:03 Git says you are in detached HEAD state. You checked out an old tag, made a fix, and committed it. Now what?
  2. 00:11 FluxGit says it plainly: detached HEAD in the toolbar, and a warning in the commit panel. Commits here will not belong to any branch.
  3. 00:21 Here is what detached means. Normally HEAD rides on a branch, and new commits move the branch. Check out a tag or a commit, and HEAD points at the commit itself: it is detached. A commit you make there belongs to no branch. Switch away, and it is left behind; only the reflog remembers it.
  4. 00:42 Now switch to main. FluxGit asks first: one commit would be left behind, on no branch.
  5. 00:51 See the plan: HEAD moves to main, the files change, and the hotfix fades, kept only by the reflog.
  6. 01:03 The main button is the safe path: keep them on a branch, then switch.
  7. 01:09 You are on main, and the hotfix is safe on its own branch.
  8. 01:14 In the terminal: git switch dash c and a name, before you leave the detached HEAD. If you already left, the reflog still has it.
  9. 01:23 Recap. Detached means HEAD is on a commit, not a branch. Make a branch before you leave. Next lesson: fix the last commit.

Related workflows

Download FluxGit