微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🔧

Git 仓库急救与智能修复专家

unfuck-my-git-state · vv0.2.1
开发 安全 A 级 作者 delorenj 公开目录收录
下载 2.8k 安装 730 收藏 10.6k 浏览 7.3k
原始页面

简介

基于标准化诊断流程的 Git 状态修复工具,通过非破坏性操作和低风险恢复流程,帮助开发者安全解决复杂仓库损坏问题。

原始描述

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.

中文使用说明 来自收录源

核心用法

unfuck-my-git-state 是一款专注于 Git 仓库状态诊断与修复的专业工具,采用"先诊断、后修复、再验证"的分阶段工作流。用户首先通过 snapshot_git_state.sh 捕获当前仓库完整状态,然后利用 guided_repair_plan.sh 根据症状(如分离 HEAD、幻影分支锁、孤立工作树等)生成针对性的非破坏性修复方案。Skill 提供多个标准化 Playbook,涵盖从工作树元数据清理到手动 HEAD 修复的完整场景,每个步骤都强调备份优先原则,并配备 regression_harness.sh 供用户在隔离环境中预演修复流程。

显著优点

该工具最大优势在于其风险控制设计理念:强制要求操作前创建快照,优先推荐非破坏性修复,仅在备份完成后才允许强制操作。相比直接使用 git reset --hard 等危险命令,它提供了系统化的症状路由机制(symptom-map.md)和验证关卡(recovery-checklist.md),确保每次修复都有明确的安全网。此外,脚本采用 set -euo pipefail 严格模式,配合完善的错误处理和临时目录自动清理机制,在技术实现层面保障了执行安全。

潜在缺点与局限性

作为 T3 来源的个人开发者项目,长期维护更新存在一定不确定性。工具设计偏向半自动化:虽然能生成精确的修复命令,但仍需用户手动复制执行,无法实现一键修复,对紧急场景下的效率有一定影响。此外,Skill 假设用户具备基础 Git internals 知识(如理解 .git/ 目录结构、symbolic-ref 等概念),对 Git 新手的友好度有限。对于极端损坏场景(如 .git/ 目录物理损坏),该工具也无法替代专业的数据恢复服务。

适合的目标群体

主要面向中高级开发者、DevOps 工程师、技术团队负责人以及频繁使用 Git 工作树(worktree)功能的专业人士。特别适合管理复杂 monorepo 或多分支并行开发环境的团队,以及需要维护长期运行 CI/CD 流水线中可能产生状态漂移的自动化环境。对于经常遇到"幽灵分支锁"、"零哈希引用"等边缘案例的开发者,这是比盲目搜索 Stack Overflow 更系统化的解决方案。

使用风险与注意事项

尽管工具本身安全规范,但用户操作风险仍是主要隐患:误执行生成的命令可能导致未提交工作丢失。虽然 Skill 强调备份,但用户可能跳过此步骤。性能方面,在超大型仓库(GB 级)上运行诊断脚本可能产生临时文件开销。依赖项风险极低(仅依赖标准 git/bash),但不同 Git 版本(尤其是 2.30 以下旧版本)对工作树处理存在细微差异,可能导致修复建议不完全匹配。建议在生产仓库首次使用前,先在测试仓库验证对应 Playbook 的行为逻辑。

技能正文


name: unfuck-my-git-state description: 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.

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:
bash scripts/snapshot_git_state.sh .
  1. Route by symptom using references/symptom-map.md.
  2. Generate non-destructive command plan:
bash scripts/guided_repair_plan.sh --repo .
  1. Apply the smallest matching playbook.
  2. Run references/recovery-checklist.md verification gate.
  3. 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/<name>, 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:
tar -czf git-metadata-backup-$(date +%Y%m%d-%H%M%S).tar.gz .git
  1. Clone fresh from remote.
  2. Recover unpushed work with reflog and cherry-pick from old clone.
  3. 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

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

其他来源收录

同一技能在不同目录的原始页面:

标签

gitdevopsdevelopment-engineeringautomationbackend

同类 Skill