# Git Recovery Playbook

From Modern Git Academy — https://moderngitacademy.com/

What to run when something has gone wrong. Read the first section before you need it.

---

## The one rule

**Committed work is almost always recoverable. Uncommitted work usually is not.**

Git's reflog records where `HEAD` has been, including commits no branch points to. That is
the safety net. It does not cover changes you never committed.

So: when something goes wrong, **commit or stash first, investigate second.**

## Start here: `git reflog`

```
git reflog
```

Shows every position `HEAD` has held, most recent first. Almost anything "lost" is in here.

```
git switch -c recovered <sha>
```

Creates a branch at that commit so it can no longer be garbage-collected.

---

## Situations

### I committed to the wrong branch

```
git reset --soft HEAD~1        # undo the commit, keep changes staged
git stash                      # set them aside
git switch correct-branch
git stash pop
git commit -m "..."
```

### I need to undo the last commit but keep the work

```
git reset --soft HEAD~1
```

Changes stay staged. Use this to redo a message or split a commit.

### I need to undo a commit that is already pushed

```
git revert <sha>
```

Creates a *new* commit that undoes the old one. Safe on shared branches because it adds
history rather than rewriting it.

### I deleted a branch that had work on it

```
git reflog                     # find the branch tip
git switch -c <branch> <sha>
```

### A rebase went wrong

```
git rebase --abort
```

Works at any point during an unfinished rebase. If the rebase already finished:

```
git reflog                     # find the pre-rebase position
git reset --hard <sha>
```

### A merge went wrong

```
git merge --abort              # while unresolved
git revert -m 1 <merge-sha>    # after it was committed
```

### I force-pushed over someone's work

The commits still exist on the machine that had them. Ask that person to run `git reflog`
and push the recovered branch. On the remote side, GitHub can restore recently deleted
branches from the repository's branch list.

### I ran `git reset --hard` and lost uncommitted changes

⚠️ Uncommitted changes are not stored anywhere in Git. There is nothing to recover them
from. Check your editor's local history — that is the only realistic route.

---

## Commands that are genuinely destructive

| Command | What is lost |
| --- | --- |
| `git restore <path>` | Uncommitted changes to that file — unrecoverable |
| `git reset --hard` | All uncommitted changes — unrecoverable |
| `git clean -fd` | All untracked files — unrecoverable |
| `git gc --prune=now` | Unreachable objects the reflog would have found |

**Never run `git gc --prune=now` while trying to recover something.**

---

Full recovery lessons: https://moderngitacademy.com/workflows/
