linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Branches & merging/detached-head

kb/branching ── Branches & merging ── intermediate

Detached HEAD

A state in which HEAD points directly at a commit rather than at a branch. Safe for reading and exploring, but any commits you make in this mode are lost unless you create a branch from them.

view as markdownaka: detached

Normally .git/HEAD contains a reference to a branch:

ref: refs/heads/main

In detached mode it holds the commit SHA directly:

a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0

HEAD is not attached to any branch. Every command that writes "to the current branch" (git commit, git pull) loses its clear target.

How you get here

bash
git switch --detach v1.0          # explicit, on a tag
git checkout HEAD~3                # implicit, on an old commit
git checkout origin/main           # implicit, on a remote branch
git checkout <sha>                 # implicit, on a specific commit

All of these are legitimate. You may want to read the code as it was at release time, find the offending commit via bisect, or run a quick local experiment.

What is safe to do here

  • Read files.
  • Run tests and builds.
  • Make local edits to the working tree (but do not commit them).

What is risky

  • Committing. The commit is created and HEAD moves to it, but no branch points at the new commit. The moment you switch to any branch, the new commit becomes unreachable and will vanish in git gc after 30 days.

If you committed here, how to recover

Create a branch from the current HEAD:

bash
git switch -c rescue
# or
git branch rescue

Now rescue points at your commits and they are safe. From there you can run git switch main && git merge rescue or cherry-pick as needed.

Pitfalls

  • Git prints a large warning when you enter detached state. Do not ignore it. Tracking down "where did those commits go" later is harder.
  • git reflog remembers everything. Even if the commits appear "lost", reflog shows their SHAs, and you can create a branch after the fact.
  • A failed git rebase sometimes leaves you in detached state. If you see an unexpected detached HEAD after a rebase, run git rebase --abort or recover via reflog.

§ команды

bash
git switch --detach v1.0

Explicitly enter detached state at a tag

bash
git switch -c rescue

Create a branch from the current detached HEAD to save commits

bash
git status

Shows 'HEAD detached at ...' instead of 'On branch ...'

§ см. также

  • branchBranchA branch in Git is a file inside `.git/refs/heads/` that holds the SHA of one commit. Creating, switching, and deleting branches are trivial operations because a branch contains exactly 41 bytes of data.
  • refloggit reflogA log of all HEAD movements on this machine. By default, entries for reachable commits live for 90 days; entries for unreachable commits live for 30. The primary recovery tool after `reset --hard`, `--amend`, `rebase`, and branch deletion. Stored locally in `.git/logs/`.
  • rebasegit rebaseRewrites the commits of a branch so they descend from a different commit. Each commit gets a new SHA; history becomes linear. Safe only on branches that no one else has seen.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies