Description
Discards local changes for tracked paths you name. Pathnames in human and --markdown output use the same quoting rules as grit status (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).
--stagedresets the index fromHEAD(or from--sourcewhen you pass it). On an unborn branch, staged paths are removed from the index.--worktreeresets the working tree. When you omit both flags, only the worktree is restored.--hunksand--linesreverse-apply only the selected diff hunks instead of whole files. Unstaged ids come fromgrit diff --unstaged --json; with--staged, ids come fromgrit diff --staged --json. Line ranges use new-side line numbers fromgrit diff --json(lines[].new), same asgrit add --lines. Cannot be combined with--source,--ours,--theirs, or--merge. A worktree hunk restore rewrites.git/indexonly 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--linesbrought it back), the index file is left as it was, likegit apply -R, and the nextgrit statusre-reads the file.--dry-runwith--hunksor--linesvalidates selection and reports paths without writing the worktree or index.--source=<rev>reads content from a commit or tree instead of the usual default (HEADfor--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,
--oursand--theirswrite index stage 2 or 3 into the worktree (filters and line endings apply) and leave the index unmerged.--mergerebuilds conflict markers from stages 1–3;--conflictpicks the marker style and implies--merge. These options cannot be combined with--stagedor--source. - A plain worktree restore (no
--staged, no--source) refuses paths that are still unmerged, for example after a conflictedgrit mergeorgrit 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,--theirsor--merge. Exclude the conflicted path (-- . ':!f.txt') to restore the rest.--stagedon 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 andgrit addthe file.--staged --worktreeresets both the index and the file toHEAD, and--sourcewithout--stagedwrites 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. |
--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. |
JSON output
Schema: v1
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). |
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. |
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.
Markdown output
Schema: v1
Pass --markdown for agent-friendly bullet lists of restored and removed paths.
Exit status
| Code | Meaning |
|---|---|
0 |
Success (success). |
1 |
Command failed (failure), including a plain or --staged restore of an unmerged path. |
2 |
Invalid flags or arguments (usage). |
See Exit codes 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.