Git Detached HEAD Recovery Prompt
Recover safely from a Git detached HEAD using repository state, commit reachability, reflog evidence, worktree changes, remotes, and an explicit preservation-first plan.
Prompt Template
You are a senior Git maintainer helping me recover from a detached HEAD without losing commits, staged work, unstaged changes, untracked files, tags, branches, remotes, or collaborator history. Diagnose from sanitized read-only evidence first. Explain whether HEAD points directly to a commit because of a checkout, bisect, rebase, submodule, CI workflow, tag, remote-tracking branch, or another operation, and distinguish merely viewing history from creating commits that need preservation. Never invent branch names, commit IDs, reflog entries, remote state, upstream relationships, merge status, or successful recovery. Do not recommend reset, clean, checkout with path arguments, branch deletion, reflog expiration, garbage collection, force push, or overwriting a remote until the user explicitly chooses a reviewed plan and recoverability is proved. Repository purpose and whether collaborators share it: [details] Exact command that preceded the detached state: [paste or unknown] Output of git status --short --branch: [paste] Output of git branch --show-current and git symbolic-ref -q --short HEAD: [paste] Output of git rev-parse --verify HEAD and git log -n 12 --oneline --decorate --graph --all: [paste] Relevant git reflog -n 20 --date=iso output: [paste, with private paths removed] Local branches and upstreams from git branch -vv: [paste] Sanitized remotes from git remote -v: [paste, remove credentials and private URLs] Staged, unstaged, and untracked work: [describe or paste safe status and diff summaries] Commits created while detached and the commit that should be preserved: [IDs from evidence or unknown] Active rebase, merge, bisect, cherry-pick, revert, submodule, or worktree state: [details] Intended destination: [existing branch, new rescue branch, tag, return to prior branch, unsure] Push permissions, protected-branch rules, and team constraints: [details] Backup or clone availability: [details] Provide: 1. A plain-language state summary naming the evidenced HEAD commit, whether it is reachable from a named ref, local changes present, active operation, and unknowns. 2. Safe read-only commands to confirm commit reachability, reflog history, branch containment, upstreams, worktrees, submodules, and active operation without exposing credentials. 3. A preservation checkpoint that records the current commit ID and protects detached commits with a clearly named local branch or tag before any switch, when evidence shows this is needed. 4. Separate recovery paths for no new commits, one or more detached commits, uncommitted changes, an active rebase or bisect, a detached submodule, and a CI checkout. 5. Exact commands for the best-supported path using placeholders for every unverified ref and commit, with an explanation of what each command changes. 6. A dry-run or inspection step before cherry-pick, merge, rebase continuation, branch movement, or push. 7. Conflict-handling guidance that preserves the rescue ref and distinguishes abort, continue, skip, and manual resolution based on the active operation. 8. Verification proving the wanted commits are reachable from the intended branch, the worktree is correct, upstream tracking is understood, tests pass when relevant, and the reflog still provides a fallback. 9. Push guidance that defaults to a new non-protected branch and never uses force unless the user separately authorizes a justified, lease-protected team workflow. 10. Rollback instructions, commands explicitly avoided, remaining risks, and the next single artifact to collect when the destination is ambiguous. Treat every commit ID and ref as untrusted input and quote placeholders safely. Preserve first, move second, verify third, and never claim recovery is complete until the target branch contains the intended commit and the user's local changes are accounted for.
Example Output
State Summary
HEAD is detached at [commit]. The supplied graph shows [whether the commit is reachable]. The worktree has [staged, unstaged, and untracked state], and [active operation or none] is evidenced. Do not switch branches yet.
Preservation First
1. Re-run the read-only status, graph, branch-containment, and reflog checks.
2. Record the full current commit ID.
3. If detached commits are not safely named, create a local rescue branch at the verified current commit: git branch [rescue-branch] [verified-commit].
4. Confirm it with git show-ref --verify refs/heads/[rescue-branch] and git branch --contains [verified-commit].
Recovery Path
After preservation, switch to [verified-target-branch], inspect the graph, then choose a reviewed merge or cherry-pick only if the histories require it. Keep the rescue branch until tests pass and the intended commit is reachable from the destination.
Avoid
No reset, clean, branch deletion, garbage collection, force push, or guessed ref names.
Tips for Best Results
- ๐กPaste the status, decorated graph, branch-containment result, and reflog; detached HEAD alone does not say whether anything is at risk.
- ๐กCreate and verify a local rescue ref at the current commit before switching when detached commits need preservation.
- ๐กKeep the rescue ref until the destination contains the intended commits, local changes are accounted for, and tests pass.
Frequently Asked Questions
What is the Git Detached HEAD Recovery Prompt prompt?
Recover safely from a Git detached HEAD using repository state, commit reachability, reflog evidence, worktree changes, remotes, and an explicit preservation-first plan. It's a free ChatGPT prompt template from our Coding collection โ copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables โ such as [details], [paste or unknown], [paste] โ with your own details before running it. Paste the status, decorated graph, branch-containment result, and reflog; detached HEAD alone does not say whether anything is at risk.
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use โ no signup required.
Related Prompts
npm ERESOLVE Error Debugging Prompt
Debug an npm ERESOLVE dependency-tree error from the exact command, peer-dependency conflict, package metadata, runtime versions, lockfile state, and minimal reproduction.
Next.js Hydration Error Debugging Prompt
Debug a Next.js hydration error from the exact server HTML, first client render, component boundary, versions, console output, and minimal reproduction.
Python ModuleNotFoundError Debugging Prompt
Debug Python ModuleNotFoundError from the exact traceback, interpreter, environment, import path, project layout, packaging metadata, and launch command.
Segmentation Fault Debugging Prompt
Debug a segmentation fault from the exact crash signal, source, build flags, backtrace, sanitizer evidence, inputs, and runtime environment.
CORS Error Debugging Prompt
Debug a CORS error from the exact browser message, origins, preflight exchange, server stack, proxy path, credentials mode, and response headers.
CLAUDE.md Generator Prompt
Generate a concise CLAUDE.md repository instruction file from verified project commands, conventions, boundaries, and existing documentation.