Detached HEAD Recovery and the Limits of Git Recovery
Preserve detached commits before switching
Detached HEAD directly identifies a commit rather than a local branch. If you commit an experiment while detached, HEAD advances but main does not. Before switching away, create a named branch using git branch rescue-detached HEAD (or switch -c to attach). The detached scenario intentionally switches away to demonstrate recovery. Never assume a warning guarantees future recovery.
Inspect unreachable candidates
After leaving the detached commit, the fixture HEAD reflog still refers to it. Verify the candidate before creating a branch. If reflogs are unavailable, git fsck --no-reflogs --unreachable can report objects not reachable from its chosen roots, including refs and index. It may show many irrelevant objects, not just your lost commit. Ignoring reflogs for inspection does not delete them. The fixture finds the detached commit before anchoring it and no longer reports it as unreachable afterward.
git reflog show HEADgit fsck --no-reflogs --unreachablecandidate=$(git rev-parse --verify "HEAD@{1}^{commit}")git show "$candidate"git branch rescue-detached "$candidate"git show rescue-detached:notes.txtRetention is conditional
Typical default reflog expiration settings are 90 days and 30 days for entries unreachable from the current tip. These are configurable thresholds, not a recovery promise. Garbage collection may remove unreachable objects when eligible; a reflog entry and an object are different things. Do not expire reflogs, prune or run aggressive maintenance when recovering data. The automated lab only previews expiration and verifies unchanged entries. It does not actually expire or prune.
git config --get gc.reflogExpiregit config --get gc.reflogExpireUnreachableKnow what Git never recorded
Untracked or unstaged bytes that were never stored as objects are not recoverable through a reflog. Previously staged blobs may exist temporarily but do not necessarily retain names or complete versions. The never-stored fixture verifies that a new draft and an unstaged edit are absent from the index and commit. If Git never captured the data, look for editor history, snapshots or independent backups. Do not experiment with destructive operations in the only copy. Practice: distinguish a missing branch name, an unreachable existing commit and a missing object.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Detached HEAD Recovery and the Limits of Git Recovery — knowledge check
Reason about reset states and safe recovery.
What advances when committing on detached HEAD?
What does fsck --no-reflogs --unreachable do?
Do typical expiration thresholds guarantee recovery until that day?
Can reflog reconstruct bytes never stored as objects?
What should you avoid during recovery?