delorenj

Unfuck My Git State

开发编程v0.2.1
作者:delorenj更新于 2026-05-11共 3 个版本 · 最新版本发布于 2026-02-06unfuck-my-git-state@clawhub_delorenj/unfuck-my-git-state
Git 辅助#Git

据 AlphaMoat 统计,截至 2026-09,Unfuck My Git State 累计下载 3,111 次、获 1 个星标,在全部 5,078 个 AI 技能中排名第 3,650 位,在「开发编程」分类(577 个)中排名第 463 位。

累计安装
102
累计下载
3,111
星标
1
全站排名
#3,650
全站前 72% · 「开发编程」前 81%
平台累计安装累计下载星标评论
ClawHub ↗1023,0831-
SkillHub ↗1013,1111—

各平台的计数口径不同,统计时间不一致。

这个技能是做什么的

流程要求先创建状态快照、根据症状选择修复方案,优先采用非破坏性步骤,并在每次修复后验证;手动修改 .git 元数据前应先备份。错误地调整 HEAD、引用或 worktree 可能影响仓库状态,因此应先明确症状和目标,避免直接执行不匹配的恢复命令。

资料发布于 2026-10-29

可以用来做什么

  • 收集 Git 状态和 worktree 诊断信息。
  • 按 detached HEAD、残留锁或引用异常等症状匹配恢复步骤。
  • 使用最小范围修复并在之后运行验证清单。

什么时候用

  • Git 报告 HEAD 矛盾、worktree 锁或引用异常时。
  • 需要诊断仓库状态并制定低风险恢复计划时。
  • 执行会修改 .git、分支引用或 worktree 的操作前,需要先备份并核实目标仓库时。

示例提示词

以下是根据技能文档生成的示例,不代表实际运行结果。

先生成仓库状态检查清单,说明需要备份的元数据和只读诊断项;不要运行修复命令。
根据这个错误拟定最小化恢复方案,标出会改动 HEAD、引用或 worktree 的步骤并等待确认。

月度数据趋势

累计下载3,111
2,1512,6673,1822026-03 · 累计下载 2,2222026-05 · 累计下载 2,5722026-06 · 累计下载 2,6972026-07 · 累计下载 2,9002026-08 · 累计下载 2,9992026-09 · 累计下载 3,11126-0326-09
星标1
0122026-03 · 星标 12026-05 · 星标 12026-06 · 星标 12026-07 · 星标 12026-08 · 星标 12026-09 · 星标 126-0326-09

2026-09 起统计方法调整:同名技能按作者分别统计,并覆盖全部技能。图中虚线为调整前的月份,与之后的数据不直接比较,也不计算跨越调整的增长。

月份累计下载环比星标
2026-093,111—1
2026-082,999+3.4%1
2026-072,900+7.5%1
2026-062,697+4.9%1
2026-052,572—1
2026-032,222—1

SKILL.md 原文

以下是作者发布的原始文件,按纯文本显示,本站未做修改或审核;其中的链接不可点击,命令请核实后再运行。抓取于 2026-10-23。

4.6 KB · ClawHub v0.2.1
---
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
展开全部(共 159 行)

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
bash scripts/snapshot_git_state.sh .
```
2. Route by symptom using `references/symptom-map.md`.
3. Generate non-destructive command plan:
```bash
bash scripts/guided_repair_plan.sh --repo .
```
4. Apply the smallest matching playbook.
5. Run `references/recovery-checklist.md` verification gate.
6. Escalate only if the gate fails.

For explicit routing:
```bash
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
bash scripts/regression_harness.sh
```

Run one scenario:

```bash
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:
```bash
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:
```bash
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:
```bash
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:
```bash
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:
```bash
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:
```bash
git show-ref --verify refs/heads/<branch>
git symbolic-ref HEAD refs/heads/<branch>
```
Fallback when `symbolic-ref` cannot be used:
```bash
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`:
```bash
tar -czf git-metadata-backup-$(date +%Y%m%d-%H%M%S).tar.gz .git
```
2. Clone fresh from remote.
3. Recover unpushed work with reflog and cherry-pick from old clone.
4. 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`

「开发编程」分类下的热门技能

数据月份:2026-09 · 下载/星标/安装数聚合自公开技能注册库(ClawHub、SkillHub),按月更新。口径说明见数据方法。