> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wit.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Undo & History

> Powerful undo capabilities for any operation

wit provides the most powerful undo capabilities of any version control system. Made a mistake? Just undo it.

## undo

Undo the last operation(s). This is wit's superpower.

```bash theme={null}
wit undo [options]
```

### Options

| Option        | Description                                |
| ------------- | ------------------------------------------ |
| `--steps <n>` | Number of operations to undo (default: 1)  |
| `--dry-run`   | Show what would be undone without doing it |

### What Can Be Undone?

* Commits
* Branch creation/deletion
* Merges
* File staging
* And more...

### Examples

```bash theme={null}
# Undo the last operation
wit undo

# Undo the last 3 operations
wit undo --steps 3

# Preview what would be undone
wit undo --dry-run
```

### Example Scenarios

**Undo an accidental commit:**

```bash theme={null}
wit commit -m "Wrong message"
wit undo  # Commit is undone, changes are back
```

**Undo branch creation:**

```bash theme={null}
wit branch wrong-name
wit undo  # Branch is deleted
```

**Undo a merge:**

```bash theme={null}
wit merge feature
# Realized you merged the wrong branch
wit undo  # Merge is reverted
```

***

## history

Show the operation history (what undo uses).

```bash theme={null}
wit history [options]
```

### Options

| Option        | Description                |
| ------------- | -------------------------- |
| `-n <number>` | Limit to last N operations |

### Examples

```bash theme={null}
# Show operation history
wit history

# Show last 5 operations
wit history -n 5
```

### Example Output

```
1. commit "Add user authentication" (2 minutes ago)
2. add src/auth.ts (5 minutes ago)
3. switch to feature-auth (10 minutes ago)
4. branch create feature-auth (10 minutes ago)
5. commit "Initial commit" (1 hour ago)
```

***

## restore

Restore files from the index or a commit.

```bash theme={null}
wit restore <file> [options]
```

### Options

| Option              | Description                                |
| ------------------- | ------------------------------------------ |
| `--staged`          | Unstage a file (keep working tree changes) |
| `--source <commit>` | Restore from a specific commit             |

### Examples

```bash theme={null}
# Discard changes to a file (restore from index)
wit restore src/index.ts

# Unstage a file
wit restore --staged src/index.ts

# Restore file from a specific commit
wit restore --source HEAD~2 src/index.ts
```

### Difference from Git

| Git Command            | wit Command                 |
| ---------------------- | --------------------------- |
| `git checkout -- file` | `wit restore file`          |
| `git reset HEAD file`  | `wit restore --staged file` |

***

## uncommit

Undo commits while keeping changes staged.

```bash theme={null}
wit uncommit [count]
```

### Arguments

| Argument | Description                            |
| -------- | -------------------------------------- |
| `count`  | Number of commits to undo (default: 1) |

### Examples

```bash theme={null}
# Undo last commit, keep changes staged
wit uncommit

# Undo last 3 commits
wit uncommit 3
```

### Use Cases

**Fix a commit message:**

```bash theme={null}
wit uncommit
wit commit -m "Better message"
```

**Add a forgotten file:**

```bash theme={null}
wit uncommit
wit add forgotten-file.ts
wit commit -m "Same message, now with forgotten file"
```

<Tip>
  For simpler cases, use `wit amend` instead:

  ```bash theme={null}
  wit amend -m "New message"
  wit add forgotten-file.ts && wit amend
  ```
</Tip>

***

## reset

Reset the current HEAD to a specific state.

```bash theme={null}
wit reset [options] [commit]
```

### Options

| Option    | Description                             |
| --------- | --------------------------------------- |
| `--soft`  | Keep changes staged                     |
| `--mixed` | Keep changes but unstage (default)      |
| `--hard`  | Discard all changes (use with caution!) |

### Examples

```bash theme={null}
# Soft reset - undo commit, keep changes staged
wit reset --soft HEAD~1

# Mixed reset - undo commit, unstage changes
wit reset HEAD~1

# Hard reset - discard all changes (DANGEROUS)
wit reset --hard HEAD~1

# Reset to specific commit
wit reset --hard abc1234
```

<Warning>
  `wit reset --hard` will permanently discard uncommitted changes. Use with caution!
</Warning>

### Difference from uncommit

| Command             | Keeps Changes? | Keeps Staged? |
| ------------------- | -------------- | ------------- |
| `wit uncommit`      | ✅ Yes          | ✅ Yes         |
| `wit reset --soft`  | ✅ Yes          | ✅ Yes         |
| `wit reset` (mixed) | ✅ Yes          | ❌ No          |
| `wit reset --hard`  | ❌ No           | ❌ No          |

***

## Comparison: undo vs uncommit vs restore vs reset

| Command    | Purpose                               | Scope                                |
| ---------- | ------------------------------------- | ------------------------------------ |
| `undo`     | Reverse any operation                 | Operations (commits, branches, etc.) |
| `uncommit` | Remove commits, keep changes          | Commits only                         |
| `restore`  | Discard/unstage file changes          | Files only                           |
| `reset`    | Move HEAD, optionally discard changes | Commits and working tree             |

### Decision Guide

* **"I want to undo the last thing I did"** → `wit undo`
* **"I want to redo my last commit differently"** → `wit uncommit` or `wit amend`
* **"I want to discard changes to a file"** → `wit restore <file>`
* **"I want to unstage a file"** → `wit restore --staged <file>`
* **"I want to go back to a specific commit"** → `wit reset [--hard] <commit>`

***

## Workflow Example

```bash theme={null}
# You made a commit with a typo
wit commit -m "Add usr authentication"

# Option 1: Use undo
wit undo
wit commit -m "Add user authentication"

# Option 2: Use amend (simpler for this case)
wit amend -m "Add user authentication"

# Later, you realize you modified the wrong file
wit restore wrong-file.ts  # Discard changes

# You staged a file by mistake
wit restore --staged accidental-file.ts
```
