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
累計下載
3111
星標
1
全站排名
#3,650
全站前 72% · 「開發程式設計」前 81%
平台累計安裝累計下載星標評論
ClawHub ↗10230831-
SkillHub ↗10131111—

各平台的計數口徑不同,統計時間不一致。

這個技能是做什麼的

流程要求先建立狀態快照、根據症狀選擇修復方案,優先採用非破壞性步驟,並在每次修復後驗證;手動修改 .git 後設資料前應先備份。錯誤地調整 HEAD、引用或 worktree 可能影響倉庫狀態,因此應先明確症狀和目標,避免直接執行不匹配的恢復命令。

資料釋出於 2026-10-29

可以用來做什麼

  • 收集 Git 狀態和 worktree 診斷資訊。
  • 按 detached HEAD、殘留鎖或引用異常等症狀匹配恢復步驟。
  • 使用最小範圍修復並在之後執行驗證清單。

什麼時候用

  • Git 報告 HEAD 矛盾、worktree 鎖或引用異常時。
  • 需要診斷倉庫狀態並制定低風險恢復計劃時。
  • 執行會修改 .git、分支引用或 worktree 的操作前,需要先備份並核實目標倉庫時。

示例提示詞

以下是根據技能文件生成的示例,不代表實際執行結果。

先生成倉庫狀態檢查清單,說明需要備份的後設資料和只讀診斷項;不要執行修復命令。
根據這個錯誤擬定最小化恢復方案,標出會改動 HEAD、引用或 worktree 的步驟並等待確認。

月度資料趨勢

累計下載3111
2151266731822026-03 · 累計下載 22222026-05 · 累計下載 25722026-06 · 累計下載 26972026-07 · 累計下載 29002026-08 · 累計下載 29992026-09 · 累計下載 311126-0326-09
星標1
0122026-03 · 星標 12026-05 · 星標 12026-06 · 星標 12026-07 · 星標 12026-08 · 星標 12026-09 · 星標 126-0326-09

2026-09 起統計方法調整:同名技能按作者分別統計,並覆蓋全部技能。圖中虛線為調整前的月份,與之後的資料不直接比較,也不計算跨越調整的增長。

月份累計下載環比星標
2026-093111—1
2026-082999+3.4%1
2026-072900+7.5%1
2026-062697+4.9%1
2026-052572—1
2026-032222—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),按月更新。口徑說明見資料方法。