← 返回
开发者工具 中文

Unfuck My Git State

Diagnose and recover broken Git state and worktree metadata with a staged, low-risk recovery flow. Use when Git reports detached or contradictory HEAD state, phantom worktree locks, orphaned worktree entries, missing refs, 0000000000000000000000000000000000000000 hashes, or branch operations fail with errors like already checked out, unknown revision, not a valid object name, or cannot lock ref.
诊断并修复损坏的 Git 状态及工作树元数据,提供分阶段、低风险的恢复流程。适用于 Git 报告 HEAD 分离或矛盾、幽灵工作树锁、孤立工作树条目、引用缺失、全零哈希,或分支操作失败(如已检出、未知版本、对象名无效、无法锁定引用等错误)。
delorenj
开发者工具 clawhub v0.2.1 1 版本 99772.1 Key: 无需
★ 1
Stars
📥 2,607
下载
💾 84
安装
1
版本
#latest

概述

unfuck-my-git-state

Recover a repo without making the blast radius worse.

Core Rules

  1. Snapshot first. Do not "just try stuff."
  2. Prefer non-destructive fixes before force operations.
  3. Treat .git/ as production data until backup is taken.
  4. Use git symbolic-ref before manually editing .git/HEAD.
  5. After each fix, run verification before proceeding.

Fast Workflow

  1. Capture diagnostics:
  2. bash scripts/snapshot_git_state.sh .
    
  3. Route by symptom using references/symptom-map.md.
  4. Generate non-destructive command plan:
  5. bash scripts/guided_repair_plan.sh --repo .
    
  6. Apply the smallest matching playbook.
  7. Run references/recovery-checklist.md verification gate.
  8. Escalate only if the gate fails.

For explicit routing:

bash scripts/guided_repair_plan.sh --list
bash scripts/guided_repair_plan.sh --symptom phantom-branch-lock

Regression Harness

Use disposable simulation tests before changing script logic:

bash scripts/regression_harness.sh

Run one scenario:

bash scripts/regression_harness.sh --scenario orphaned-worktree

Playbook A: Orphaned Worktree Metadata

Symptoms:

  • git worktree list shows a path that no longer exists.
  • Worktree entries include invalid or zero hashes.

Steps:

git worktree list --porcelain
git worktree prune -v
git worktree list --porcelain

If stale entries remain, back up .git/ and remove the specific stale folder under .git/worktrees/, then rerun prune.

Playbook B: Phantom Branch Lock

Symptoms:

  • git branch -d or git branch -D fails with "already used by worktree".
  • git worktree list seems to disagree with branch ownership.

Steps:

git worktree list --porcelain

Find the worktree using that branch, switch that worktree to another branch or detach HEAD there, then retry the branch operation in the main repo.

Playbook C: Detached or Contradictory HEAD

Symptoms:

  • git status says detached HEAD unexpectedly.
  • git branch --show-current and git symbolic-ref -q HEAD disagree.

Steps:

git symbolic-ref -q HEAD || true
git reflog --date=iso -n 20
git switch <known-good-branch>

If branch context is unknown, create a rescue branch from current commit:

git switch -c rescue/$(date +%Y%m%d-%H%M%S)

Then reconnect to the intended branch after investigation.

Playbook D: Missing or Broken Refs

Symptoms:

  • unknown revision, not a valid object name, or cannot lock ref.

Steps:

git fetch --all --prune
git show-ref --verify refs/remotes/origin/<branch>
git branch -f <branch> origin/<branch>
git switch <branch>

Use reflog to recover local-only commits before forcing branch pointers.

Last Resort: Manual HEAD Repair

Only after backup of .git/.

Preferred:

git show-ref --verify refs/heads/<branch>
git symbolic-ref HEAD refs/heads/<branch>

Fallback when symbolic-ref cannot be used:

echo "ref: refs/heads/<branch>" > .git/HEAD

Immediately run the verification gate.

Verification Gate (Must Pass)

Run checks in references/recovery-checklist.md. Minimum bar:

  • git status exits cleanly with no fatal errors.
  • git symbolic-ref -q HEAD matches intended branch.
  • git worktree list --porcelain has no missing paths and no zero hashes.
  • git fsck --no-reflogs --full has no new critical errors.

Escalation Path

  1. Archive .git:
  2. tar -czf git-metadata-backup-$(date +%Y%m%d-%H%M%S).tar.gz .git
    
  3. Clone fresh from remote.
  4. Recover unpushed work with reflog and cherry-pick from old clone.
  5. Document failure mode and add guardrails to automation.

Automation Hooks

When building worktree tooling (iMi, scripts, bots), enforce:

  • preflight snapshot and state validation
  • post-operation verification gate
  • hard stop on HEAD/ref inconsistency
  • explicit user confirmation before destructive commands

Resources

  • Symptom router: references/symptom-map.md
  • Verification checklist: references/recovery-checklist.md
  • Diagnostic snapshot script: scripts/snapshot_git_state.sh
  • Guided plan generator: scripts/guided_repair_plan.sh
  • Disposable regression harness: scripts/regression_harness.sh

版本历史

共 1 个版本

  • v0.2.1 当前
    2026-03-28 13:17 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 668 📥 323,975
content-creation

Fal Text-to-Image

delorenj
利用 fal.ai 的 AI 模型生成、重塑及编辑图像。支持文生图、图像重塑以及局部重绘/编辑。
★ 1 📥 2,547
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 66 📥 179,998