# grit restore

> Restore working tree and/or index paths from HEAD, the index, or another revision.

## Synopsis

```text
grit restore [--staged] [--worktree] [--hunks <id,...>] [--lines <path:start-end>] [--dry-run] [--source=<rev>] [--ours|--theirs|--merge] [--conflict=merge|diff3|zdiff3] [--pathspec-from-file <file|->] [--pathspec-file-nul] -- <path>…
```

## Description

Discards local changes for tracked paths you name. Pathnames in human and `--markdown` output use the same quoting rules as [`grit status`](https://grit-scm.com/docs/status/index.md) (see **Description** there). By default, `grit restore` resets the **working tree** to match the **index** (the same idea as unstaging worktree edits while keeping the index as-is).

- `--staged` resets the **index** from `HEAD` (or from `--source` when you pass it). On an unborn branch, staged paths are removed from the index.
- `--worktree` resets the **working tree**. When you omit both flags, only the worktree is restored.
- `--hunks` and `--lines` reverse-apply only the selected diff hunks instead of whole files. Unstaged ids come from `grit diff --unstaged --json`; with `--staged`, ids come from `grit diff --staged --json`. Line ranges use new-side line numbers from `grit diff --json` (`lines[].new`), same as `grit add --lines`. Cannot be combined with `--source`, `--ours`, `--theirs`, or `--merge`. A worktree hunk restore rewrites `.git/index` only when it updates a path's cached stat data, which it checks once the selected ids cover every remaining hunk of a file; while the file still differs from the index (or when `--lines` brought it back), the index file is left as it was, like `git apply -R`, and the next `grit status` re-reads the file.
- `--dry-run` with `--hunks` or `--lines` validates selection and reports paths without writing the worktree or index.
- `--source=<rev>` reads content from a commit or tree instead of the usual default (`HEAD` for `--staged`, the index for the worktree).
- Paths that are missing in the source are removed from the target. Untracked files are never touched.
- Index entries with the skip-worktree bit (for example after `git update-index --skip-worktree`) are left out of restore, the same as Git: `grit restore .` does not rewrite them or clear the flag.
- During a merge conflict, `--ours` and `--theirs` write index stage 2 or 3 into the worktree (filters and line endings apply) and leave the index unmerged. `--merge` rebuilds conflict markers from stages 1–3; `--conflict` picks the marker style and implies `--merge`. These options cannot be combined with `--staged` or `--source`.
- A plain worktree restore (no `--staged`, no `--source`) refuses paths that are still unmerged, for example after a conflicted `grit merge` or `grit stash pop`: there is no single index version to restore. It fails before writing anything, even when other matched paths could be restored (`grit restore .` included), and tells you to pick `--ours`, `--theirs` or `--merge`. Exclude the conflicted path (`-- . ':!f.txt'`) to restore the rest. `--staged` on its own refuses unmerged paths the same way (Git would drop the conflict stages and leave the markers in the file, and grit's auto-staging commit would then record them); to resolve, edit out the markers and `grit add` the file. `--staged --worktree` resets both the index and the file to `HEAD`, and `--source` without `--staged` writes only the worktree and leaves the conflict stages in place.

## Options

| Option | Description |
| --- | --- |
| `<path>…` | One or more pathspecs (required for full-file restore unless `--pathspec-from-file` is set). Optional with `--hunks` / `--lines` to narrow the diff scan. Attribute pathspecs such as `:(attr:myattr)` use the same `.gitattributes` stack as [`grit log`](https://grit-scm.com/docs/restore/log.md/index.md). |
| `--pathspec-from-file` | Read pathspecs from a file, or `-` for stdin (same rules as Git). |
| `--pathspec-file-nul` | With `--pathspec-from-file`, entries are NUL-separated instead of line-based. |
| `-S`, `--staged` | Restore the index (or unstage selected hunks with `--hunks` / `--lines`). |
| `-W`, `--worktree` | Restore the working tree (default when `--staged` is absent). |
| `--hunks` | Discard or unstage only the listed hunk ids (comma-separated, repeatable). |
| `--lines` | Discard or unstage only new-side line ranges (`path:start-end`, repeatable). Mix with `--hunks`. |
| `--dry-run` | With `--hunks` or `--lines`, report paths that would change without writing. |
| `-s`, `--source` | Commit or tree to restore from. |
| `--ours` | Unmerged paths only: check out our side (stage 2) in the worktree. |
| `--theirs` | Unmerged paths only: check out their side (stage 3) in the worktree. |
| `--merge` | Unmerged paths only: recreate conflict markers in the worktree (default style from `merge.conflictStyle`). |
| `--conflict` | With `--merge`: `merge`, `diff3`, or `zdiff3` marker style. |

## Examples

Discard unstaged edits to a file (worktree ← index):

```console
$ grit restore README.md
Restored README.md
```

Unstage a file (index ← HEAD):

```console
$ grit restore --staged README.md
Restored README.md
```

Discard one unstaged hunk by id:

```console
$ grit diff --unstaged --json -- f.txt | jq -r '.files[0].hunks[0].id'
a1b2c3d4e5f6
$ grit restore --hunks a1b2c3d4e5f6 -- f.txt
Restored f.txt
```

Restore the working tree from the previous commit:

```console
$ grit restore --source=HEAD~1 -- README.md
Restored README.md
```

During a merge, inspect one side or bring conflict markers back:

```console
$ grit restore --ours -- README.md
Restored README.md

$ grit restore --merge --conflict=diff3 -- README.md
Restored README.md
```

A plain or `--staged` restore on a conflicted path names the options that work:

```console
$ grit restore README.md
error: path 'README.md' is unmerged
hint: take one side with `grit restore --ours README.md` or `grit restore --theirs README.md`, bring the conflict markers back with `grit restore --merge README.md`, or edit out the markers and run `grit add README.md`
```

## JSON output

Schema: [v1](https://grit-scm.com/docs/restore/schemas/restore/v1.json/index.md)

Pass `--json` for scripting:

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `restored` | array of strings | Paths created or updated (non-UTF-8 names use reversible `grit+pathhex:` encoding, same as [`grit status`](https://grit-scm.com/docs/restore/status.md/index.md)). |
| `removed` | array of strings | Paths removed because the source had no entry (same encoding as `restored`). |
| `path_collisions` | array of strings | On a case-insensitive work tree, index paths that share one dentry (informational; same shape as `grit switch` / `grit clone`). Omitted when empty. |
| `restored_hunks` | object | When `--hunks` was used: map of path → hunk id strings. |
| `restored_lines` | object | When `--lines` was used: map of path → range strings (`start` or `start-end`). |
| `dry_run` | boolean | True when `--dry-run` was passed with hunk/line selection. |

Example:

```json
{
  "restored": ["README.md"],
  "removed": [],
  "restored_hunks": {},
  "restored_lines": {},
  "dry_run": false
}
```

When a plain or `--staged` restore refuses an unmerged path, stdout with `--json` is a single error object with `kind` `unmerged_paths`, a `paths` array of the unmerged paths and a `hint` naming the `grit restore --ours` / `--theirs` / `--merge` commands and `grit add`. See the [error schema](https://grit-scm.com/docs/restore/schemas/error/v1.json/index.md).

## Markdown output

Schema: [v1](https://grit-scm.com/docs/restore/schemas/restore/v1.json/index.md)
Pass `--markdown` for agent-friendly bullet lists of restored and removed paths.




```text
## Restore

### Restored
- `tracked.txt`
```

## Exit status

| Code | Meaning |
| ---: | --- |
| `0` | Success ([`success`](https://grit-scm.com/docs/exit-codes/index.md)). |
| `1` | Command failed ([`failure`](https://grit-scm.com/docs/exit-codes/index.md)), including a plain or `--staged` restore of an unmerged path. |
| `2` | Invalid flags or arguments ([`usage`](https://grit-scm.com/docs/exit-codes/index.md)). |

See [Exit codes](https://grit-scm.com/docs/exit-codes/index.md) for shared rules and structured `--json` `kind` values.

When another process holds `.git/index.lock`, `grit restore` fails with exit `1` (`kind` `lock_held` under `--json`) before restoring any path, so the worktree and index are unchanged.


## See also

[grit add](https://grit-scm.com/docs/add/index.md), [grit status](https://grit-scm.com/docs/status/index.md)
