---
name: safe-git-recovery
description: Recovers from git mistakes without losing work using reflog, revert versus reset, restoring deleted branches, undoing bad merges or rebases and finding lost stashes, backing up first and asking before anything destructive. Use when the user broke or lost something in git.
---

# Safe Git Recovery

Most git mistakes are recoverable because git keeps old commits around for a while. Your job is to find the lost state, secure it, and only then fix things. Speed is not the goal; not losing work is.

## 1. Stop and look

Do not run anything that rewrites history or discards changes yet. Gather the facts:

```bash
git status
git branch -vv
git log --oneline --graph --decorate -20
git reflog -30
git stash list
```

Ask the user what they were doing and what they expect to see. Establish whether the commits involved were pushed (`git branch -r --contains <sha>`), because pushed history is shared and needs a different approach.

## 2. Make a backup before touching anything

Create a safety ref at the current state so every later step can be undone:

```bash
git branch backup/before-recovery-$(date +%Y%m%d-%H%M%S)
```

If there are uncommitted changes you want to preserve, do not clean them away. Copy them out first (`git stash push -u -m "backup"` is acceptable only if the user agrees, as it moves their files) or tell the user to copy the folder.

## 3. Pick the right tool

Undo a commit that is already pushed or shared:

```bash
git revert <sha>              # adds a new commit that cancels it; safe for shared history
git revert -m 1 <merge-sha>   # undo a merge commit, keeping mainline as parent 1
```

Undo local, unpushed commits (history rewrite, ask first):

```bash
git reset --soft HEAD~1       # undo the commit, keep changes staged
git reset --mixed HEAD~1      # undo the commit, keep changes unstaged
```

`git reset --hard` throws away uncommitted work; run it only with the user's explicit approval and after the backup branch exists.

Recover a deleted branch or a commit that "disappeared":

```bash
git reflog                                # find the last sha the branch pointed at
git branch restored-feature <sha>         # recreate it under a safe name
```

Recover after a bad rebase or reset: `git reflog` shows entries such as `rebase (finish)` and `reset: moving to ...`. Find the entry just before the mistake (`HEAD@{n}`), then inspect it with `git log --oneline HEAD@{n}` and recreate a branch from it before moving anything.

Undo a bad merge that is not pushed: look at the reflog for the pre-merge head and create a branch there. If the merge is still in progress, `git merge --abort` is the way out.

Recover a dropped stash or a stash lost after a reset:

```bash
git fsck --no-reflogs --lost-found | grep commit    # dangling commits (stashes show up here)
git show <sha>                                      # inspect candidates
git stash apply <sha>                               # apply a recovered stash commit
```

Recover a single file from an older commit. This overwrites the working-tree file. If it has uncommitted edits, ask first or inspect without touching it: `git show <sha>:path/to/file > /tmp/file.old`.

```bash
git restore --source=<sha> -- path/to/file
```

Lost uncommitted work that was never added or stashed cannot be recovered by git; check editor local history and the OS trash instead.

## 4. Verify before declaring success

- Compare the recovered state with what the user expects: `git diff backup/... restored-branch`, `git log --stat`.
- Run the build and tests if the recovery touched code.
- Only now move the real branch (`git switch main`, `git branch -f` or `git reset`), and show the user the exact command first.

## 5. Report

```
What happened: <one or two lines, based on the reflog>
Backup: <branch/tag name and sha>
Recovered: <branch name, sha, what it contains>
Commands run: <list>
Needs your decision: <e.g. force-push to fix the remote, delete the backup later>
```

## Pitfalls

- Do not force-push (`--force`, `--force-with-lease`) to repair a remote without asking; it rewrites what others have pulled.
- Do not run `git gc --prune=now`, `git reflog expire` or `git clean -fd` during recovery; they delete the very data you need.
- Do not `reset --hard` to "get back to a clean state" before looking at the reflog.
- Do not use `revert` on a shared branch when the goal is to hide a leaked secret; rotate the secret instead, and discuss history rewriting with the user.
- Reflog entries expire (typically 90 days for reachable, 30 for unreachable) and are local to each clone; act promptly.
- Do not delete the backup branch yourself; leave that to the user.

Source: [AnyModel skills catalog](https://anymodel.org/en/skills) · License: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
