Skip to content
tools for techies
Colour theme

Lost code finder

Digs through reflog, dangling commits and stashes to find lost work, then restores it to a safe branch.

Claude Code skill codemaman-git-recover, works with Git and GitHub.

Man overboard

Install it

Copies the skill into ~/.claude/skills:

curl -fsSL https://toolsfortechies.dev/install.sh | bash -s -- codemaman-git-recover

Or download the zip and unzip it into ~/.claude/skills. Restart Claude Code afterwards.

Try it

Ask Claude
“I reset the wrong thing and lost my commits”
Or run
/codemaman-git-recover

Before you start

You'll need
git. The gh CLI is optional, for finding commits kept on GitHub.
What it won't do without you
Restores to a new branch first. Never prunes, cleans or force-pushes.
Read the SKILL.md
# Git recovery

Almost everything committed in git can be recovered. Stay calm, change nothing until you've found it, and restore without disturbing the current work.

## 1. Stop and secure (read-only)
- Do **not** run `git gc`, `git prune`, `git clean`, `git reset --hard`, or more commits on the affected branch until recovery is done.
- Check for uncommitted changes with `git status`. If there are any, protect them first (`git stash push -u -m "before recovery"`, or commit them to a temporary branch) and say you did.
- Ask what was lost if it isn't clear: which branch, roughly when, and what happened (reset, rebase, branch deleted, amend, merge, force-pull, dropped stash, a checkout that overwrote files). Never-committed changes are a different story: see step 4.

## 2. Search for it
Run these, newest evidence first, and show the user the relevant rows:
- **Reflog of HEAD**: `git reflog --date=relative -n 100` records where HEAD has been, including before resets and rebases. Look for entries such as `reset: moving to`, `rebase (finish)`, `commit (amend)`, or `checkout: moving from <branch>`.
- **Reflog of one branch**: `git reflog show <branch> --date=relative`. Works for a branch that still exists. For a deleted branch, use the HEAD reflog and look for the last `checkout: moving from <deleted-branch> to ...`, whose left-hand side is the tip.
- **Remote copies**: `git branch -r --contains <sha>`, `git ls-remote origin`, and `git log origin/<branch>`. The remote or a teammate's clone may still hold it. Also check a GitHub PR: `gh pr view <n> --json commits` (a force-pushed PR keeps its old commits).
- **Dangling objects**: `git fsck --lost-found --no-reflogs` (or `git fsck --unreachable | grep commit`) lists commits no ref points to. Show each with `git show -s --format='%h %ad %an %s' <sha>`.
- **Dropped stashes**: `git fsck --unreachable | grep commit` then look for commits whose message starts with `WIP on` or `On <branch>:`. A stash is a commit.
- **Specific content**: `git log --all --oneline -S'<snippet>'` finds commits adding or removing a string, and `git log --all -- <path>` finds a file's history, including deleted files.
- Use the timestamps and messages to identify candidates. When there are several, compare with `git diff <candidate>~..<candidate> --stat` and `git show <candidate> --stat`.

## 3. Confirm it's the right thing
Before changing anything, show the user the candidate: its SHA, date, author, message, files changed, and a short log (`git log --oneline -n 10 <sha>`). Ask them to confirm it's what they were missing. If the lost work spans several commits, find the tip that contains them all (`git branch --contains` won't work on dangling commits; use `git merge-base --is-ancestor <old> <tip>`).

## 4. Restore without damage
Choose the least destructive route and explain it in a sentence:
- **Safest, always works**: make a new branch at the commit, leaving everything else alone: `git branch recovered/<name> <sha>` (or `git switch -c recovered/<name> <sha>`). Make this the default, even if the user wants the original branch back, because it is a safe checkpoint.
- **Restore a deleted branch**: `git branch <name> <sha>`.
- **Undo a bad reset, rebase or amend on the current branch**: first create the recovered branch as a safety net, then move the branch only with the user's yes: `git reset --hard <sha>` is safe only when the working tree is clean (it was stashed in step 1) and the user confirms. Prefer `git reset --keep <sha>` when there are local changes.
- **Pick some commits back**: `git cherry-pick <sha>` (or a range), when the branch has moved on and they only want specific changes.
- **One file back**: `git restore --source=<sha> -- <path>`, or `git show <sha>:<path> > <path>`.
- **Dropped stash**: `git stash apply <sha>` (or `git branch stash-recovered <sha>`), and keep it until confirmed.
- **Overwritten but never committed work**: git can't recover what it never saw. Say so plainly, then try what might help: `git fsck --lost-found` also finds blobs from staged (`git add`ed) files (`git show <blob-sha>`), plus editor local history (VS Code Timeline, JetBrains Local History), the OS trash, and backups.
- If the remote still has the work and the local branch is wrong, `git fetch` and compare before resetting.

## 5. Verify and report
- Check that the restored work is there: `git log --oneline -n 10`, `git diff <sha> HEAD --stat` (should be empty if restored exactly), run the tests or build if the user wants it.
- Don't push. If a push is needed after restoring a rewritten branch, tell the user and offer `git push --force-with-lease` for them to confirm; never plain `--force`.
- Report: what was lost and how, what was found (SHA, date, message), what you did (branch name or command), the previous tip SHA so this step is reversible, and how long unreachable commits survive (by default about 30 days for unreachable reflog entries and 90 days for reachable ones; `git gc` can remove them sooner).
- Offer one prevention tip that fits what happened, for example `git switch -c backup/<name>` before risky rebases, or `git config --global rebase.autoStash true`. Ask before changing any config.

## Rules
- Make a safety branch before moving any existing branch.
- Never delete reflog entries, run `git gc --prune=now`, `git reflog expire`, or `git clean -fdx` as part of recovery.
- Don't guess between candidate commits: show them and ask.
- Be honest when something is unrecoverable.
Open the raw file

More skills like this