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.

MIT-0 · Free to use, modify, and redistribute. No attribution required.
1 · 2k · 4 current installs · 4 all-time installs
MIT-0
Security Scan
VirusTotalVirusTotal
Benign
View report →
OpenClawOpenClaw
Benign
high confidence
Purpose & Capability
The name/description match the included scripts and references. The scripts take snapshots, detect symptoms, print recovery plans, and provide a disposable regression harness — all directly related to diagnosing and repairing Git metadata. No unrelated credentials, binaries, or external services are requested.
Instruction Scope
SKILL.md and the scripts confine actions to the target repository: creating a snapshot under <repo>/.git-state-snapshots, running git commands, and printing recommended commands. The guided script only recommends fixes (it does not execute destructive commands). The documentation and playbooks explicitly require backups and verification before destructive actions.
Install Mechanism
No install spec is present; this is instruction-only with bundled scripts. Nothing is downloaded or installed from external URLs. The scripts are plain Bash and operate locally.
Credentials
No environment variables, credentials, or config paths are required. The scripts read repository metadata (e.g., .git/HEAD, .git/worktrees) which is appropriate for the stated purpose.
Persistence & Privilege
The skill is not forced-always and does not request persistent system-level privileges. It writes snapshots under the repo top-level (.git-state-snapshots) and the user-facing docs show where backups will be created; these are proportionate to diagnostic and recovery activities.
Assessment
This skill appears coherent and repository-scoped, but review and run it consciously: 1) Inspect the scripts locally before executing. 2) Run the regression harness in a disposable environment first if you want to validate behavior. 3) The tool creates snapshots under the repo (./.git-state-snapshots) and may instruct you to create tar backups — make sure you have disk space and that storing such backups in the current directory is acceptable. 4) Follow the recommended verification gate and backup steps before performing any of the 'last resort' commands (editing .git/HEAD, force-moving branches, or removing .git/worktrees). 5) Because this skill executes Bash scripts against your repo, avoid running it as a privileged user or against a repo containing sensitive unpushed data unless you have backups.

Like a lobster shell, security has layers — review code before you run it.

Current versionv0.2.1
Download zip
latestvk97dgjp0f1sd7ncm8kkm9qmxyn80madv

License

MIT-0
Free to use, modify, and redistribute. No attribution required.

SKILL.md

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

Files

8 total
Select a file
Select a file to preview.

Comments

Loading comments…