# grit stash

> Save and restore shelved work.

## Synopsis

```text
grit stash [-m <msg>] [-u] [-- path…]
grit stash push [-m <msg>] [-u] [-S] [-k] [--hunks <id,...>] [--lines <path:start-end>] [--dry-run] [--pathspec-from-file <file|->] [--pathspec-file-nul] [-- path…]
grit stash list
grit stash show [<n>|<commit>] [--stat] [-p] [-u]
grit stash apply [<n>|<commit>] [--index]
grit stash pop [<n>|<commit>] [--index]
grit stash branch <name> [<n>|<commit>]
grit stash drop [<n>]
grit stash clear [--force] [--dry-run]
```

## Description

Shelve uncommitted changes and restore them later. With no subcommand, `grit stash` saves local changes (same as `grit stash push`), resets the working tree and index to `HEAD`, and records a stash entry compatible with Git's `refs/stash` format. Skip-worktree paths are not stashed or reset on push, and the bit stays set through pop, matching Git. Unreadable worktree directories produce a warning naming the path; stash still saves readable changes and does not treat files under those directories as deleted. Like Git, dirty content inside a checked-out submodule whose gitlink commit is unchanged does not count as a local change to save (push reports that there is nothing to stash); a submodule whose HEAD moved away from the recorded gitlink is still stashed.

Like Git, stash never signs the commits it records, even when `commit.gpgsign` is true, so a configured signing program is not run on push.

Pass `--` followed by pathspecs (or `--pathspec-from-file`) on push to stash only matching paths; other staged and unstaged changes stay in place. The stash commit still records the full index on its index parent; the working-tree parent and optional untracked parent are limited to those paths, matching `git stash push -- <pathspec>`. Attribute pathspecs such as `:(attr:myattr)` use the same `.gitattributes` stack as [`grit log`](https://grit-scm.com/docs/stash/log.md/index.md).

Stash indices follow Git: `0` is the newest entry. You can pass an integer or `stash@{n}`. An all-decimal token is always an index (never a short object id). Any other revision resolves to a stash commit id: `apply` and `branch` accept dangling stash commits (for example after `drop`), and `pop` accepts a commit id only while that commit is still on the stack.

`grit stash pop` applies the entry and removes it when there are no conflicts. When applying would leave merge conflicts, the stash entry is kept and the command exits non-zero; conflicting paths are listed. There is no merge to continue: edit the files, mark them resolved with `grit add <file>`, then remove the kept entry with `grit stash drop` (or pick a side with `grit restore --ours|--theirs <file>`). `grit stash apply` stops the same way but never drops the entry.

`apply` and `pop` preserve the exact tracked filename bytes, including literal names beginning with `grit+pathhex:` and non-UTF-8 names on Unix. If a stashed change would overwrite local edits to that file, the operation fails before changing files and keeps the stash. Conflict paths use reversible `grit+pathhex:` encoding when needed; Rust callers can recover the original bytes with `repo_relpath::storage_bytes`.

When a stash was created with `-u` / `--include-untracked`, `apply` and `pop` refuse to write stashed untracked files onto a path that already exists on disk (visible untracked, ignored, or tracked content you added after stashing). The command exits non-zero, the stash entry is kept, and your existing file bytes are unchanged.

On an interactive terminal (human output, not `--json` or `--markdown`), push shows a live **Staging** line while it hashes local changes and a **Checkout** line while it resets them, and apply and pop show a **Checkout** line; nothing is drawn when stderr is not a TTY.

Push, apply, and pop either complete or change nothing. A pop or apply that includes untracked files finishes restoring every path (tracked and untracked) before it writes the index or drops the stash entry; if an object cannot be read (for example because background repack moved it while the command was running), the worktree and index are rolled back and the stash entry stays. Pressing Ctrl+C, or sending `SIGTERM` or `SIGHUP` on Unix, while the index or work tree is being updated stops the command with `error: operation cancelled`; `HEAD`, the index and the work tree are left as they were, no `.git/index.lock` is left behind, and the same stash command can simply be run again. This works the same in a Windows console, where Ctrl+Break counts as Ctrl+C. A second interrupt terminates grit at once, without that cleanup; only `SIGKILL` skips rollback. Staging may leave unreachable loose blobs if you interrupt mid-write; they are harmless.

## Options

| Option | Description |
| --- | --- |
| `-m`, `--message` | Message stored on the stash reflog (push / bare `grit stash`). |
| `-u`, `--include-untracked` | Include untracked files in the stash (not with `--hunks` / `--lines` or `--staged`). |
| `-S`, `--staged` | Stash only index changes vs `HEAD`; unstaged edits on other paths remain (not with `-u`, `--hunks`, or `--lines`). |
| `-k`, `--keep-index` | Stash tracked changes but leave the index and worktree matching staged content afterward. |
| `--hunks` | Stash only listed unstaged hunk ids from `grit diff --unstaged --json`. |
| `--lines` | Stash only unstaged line ranges (`path:start-end`, same as `grit add --lines`). |
| `--dry-run` | Preview what would be stashed; no objects, refs, index, or worktree updates. |
| `-n`, `--limit` | Cap path lists in `--dry-run` preview output. |
| `<path>…` | Limit what is stashed (after `--`); with `--staged`, only matching index changes are saved and reset. |
| `--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. |
| `--index` | On `apply` / `pop`, restore the stashed index as well as the worktree. Changes that were unstaged when you stashed come back unstaged, including when nothing was staged at all. |
| `<n>` | Stash index for `show`, `apply`, `pop`, or `drop` (default `0`). |
| `--stat` | Show a diffstat in `grit stash show` (default when `-p` is not set). |
| `-p`, `--patch` | Show the full patch in `grit stash show` instead of a diffstat. |
| `-u`, `--include-untracked` | On `show`, include untracked files from the stash U parent (same as `git stash show -u`). |
| `push` | Save changes explicitly (same flags as bare `grit stash`). |
| `list` | List stash entries. |
| `show` | Show a diffstat or patch for one entry. |
| `apply` | Apply an entry without removing it. |
| `pop` | Apply an entry and drop it when clean. |
| `drop` | Remove an entry without applying it. |
| `branch` | Create a branch at the stash base commit, check it out, apply with `--index` (staged changes come back staged, unstaged ones unstaged), and drop the entry on success. |
| `clear` | Remove every stash entry. Requires `--force`; without it the command exits non-zero and reports how many entries would be cleared. `--dry-run` reports the count without deleting. |

## Examples

Save work in progress:

```console
$ grit stash -m "wip feature"
Saved working directory and index state On main: wip feature a1b2c3d
```

List entries:

```console
$ grit stash list
stash@{0} a1b2c3d: On main: wip feature
```

Show the newest stash as a diffstat:

```console
$ grit stash show
 f.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
```

Human and `--markdown` diffstat output C-quotes C0 control characters in path names (including ESC, tab, and newline), the same as [`grit status`](https://grit-scm.com/docs/stash/status.md/index.md) and other diffstat commands; `--json` `stat.files[].path` values stay raw UTF-8 strings (JSON escapes controls as `\uXXXX`).

Pop the newest entry:

```console
$ grit stash pop
Dropped stash@{0} (applied)
```

When pop leaves conflicts:

```console
$ grit stash pop
Stash pop left conflicts in:
  f.txt
The stash entry was kept.
→ resolve conflicts, grit add <file>, then grit stash drop
$ echo resolved > f.txt
$ grit add f.txt
Staged f.txt
$ grit stash drop
Dropped stash@{0}
```

When pop would overwrite an existing path from a `-u` stash:

```console
$ grit stash pop
stash untracked restore blocked: 'notes.txt' already exists on disk
```

## JSON output

Schema: [v1](https://grit-scm.com/docs/stash/schemas/stash/v1.json/index.md) · [stash.push v1](https://grit-scm.com/docs/stash/schemas/stash.push/v1.json/index.md) · [stash.list v2](https://grit-scm.com/docs/stash/schemas/stash.list/v2.json/index.md), [v1](https://grit-scm.com/docs/stash/schemas/stash.list/v1.json/index.md) · [stash.show v1](https://grit-scm.com/docs/stash/schemas/stash.show/v1.json/index.md) · [stash.apply v2](https://grit-scm.com/docs/stash/schemas/stash.apply/v2.json/index.md), [v1](https://grit-scm.com/docs/stash/schemas/stash.apply/v1.json/index.md) · [stash.pop v1](https://grit-scm.com/docs/stash/schemas/stash.pop/v1.json/index.md) · [stash.drop v2](https://grit-scm.com/docs/stash/schemas/stash.drop/v2.json/index.md), [v1](https://grit-scm.com/docs/stash/schemas/stash.drop/v1.json/index.md) · [stash.branch v2](https://grit-scm.com/docs/stash/schemas/stash.branch/v2.json/index.md), [v1](https://grit-scm.com/docs/stash/schemas/stash.branch/v1.json/index.md) · [stash.clear v1](https://grit-scm.com/docs/stash/schemas/stash.clear/v1.json/index.md)

Pass `--json` for stable, scripting-friendly output.

### `grit stash` / `grit stash push`

When something was stashed:

```json
{
  "oid": "6e513d5cd6ab8d618238e22d7ca55d942b55964a",
  "message": "On main: wip feature",
  "mode": "all"
}
```

Partial stash (`--hunks` / `--lines`) also includes `stashed_hunks` and `stashed_lines` maps (same shape as `grit add --json`).

`--dry-run` preview (no `oid`):

```json
{
  "dry_run": true,
  "mode": "staged_only",
  "paths": ["staged_new.txt"],
  "untracked": []
}
```

When there was nothing to save:

```json
{
  "stashed": false
}
```

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `oid` | string | Full object id of the stash commit (success object only). |
| `message` | string | Reflog message (for `-m`, `On <branch>: …`; otherwise Git-style `WIP on …`). |
| `mode` | string | `all`, `staged_only`, or `keep_index`. Omitted when `all`. |
| `dry_run` | boolean | Always `true` on the preview object; omitted on successful save (`false`). |
| `paths` | array of strings | Tracked paths that would be stashed (`--dry-run` preview only). |
| `untracked` | array of strings | Untracked paths in a dry-run preview when `-u` is set. |
| `stashed_hunks` | object | Map of repository-relative path → hunk id strings stashed (partial `--hunks` only; same shape as `grit add --json`). Omitted when empty. |
| `stashed_lines` | object | Map of path → line range labels stashed (partial `--lines` only). Omitted when empty. |
| `paths` | array of strings | Resolved pathspecs when push was limited with `-- path…` or `--pathspec-from-file`. Omitted when empty. |
| `stashed` | boolean | Present and `false` only on the empty-worktree result. |

### `grit stash list`

```json
{
  "action": "list",
  "entries": [
    {
      "index": 0,
      "oid": "6e513d5cd6ab8d618238e22d7ca55d942b55964a",
      "message": "On main: second"
    }
  ]
}
```

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `action` | string | Always `"list"`. |
| `entries` | array | Stash entries, newest first. |
| `entries[].index` | number | Index matching `stash@{index}`. |
| `entries[].oid` | string | Full stash commit id. |
| `entries[].message` | string | Reflog message. |

### `grit stash show`

Diffstat (default):

```json
{
  "index": 0,
  "include_untracked": true,
  "stat": {
    "files_changed": 1,
    "insertions": 1,
    "deletions": 1,
    "files": [
      {
        "path": "f.txt",
        "status": "modified",
        "insertions": 1,
        "deletions": 1,
        "binary": false
      }
    ]
  }
}
```

With `--patch`, a `patch` object with the same shape as [`grit diff`](https://grit-scm.com/docs/diff/index.md) is included instead of `stat`.

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `index` | number | Stash index shown. |
| `include_untracked` | boolean | Whether `-u` / `--include-untracked` was used (omitted when false). |
| `stat` | object | Diffstat summary (when `-p` is not set). |
| `patch` | object | Full diff outcome (when `-p` is set). |

### `grit stash branch`

```json
{
  "branch": "from-stash",
  "base": "a1b2c3d4e5f6789012345678901234567890abcd",
  "oid": "d78432c4772cb8509513bca138a60d13e5ad50fd",
  "index": 0,
  "conflicts": false
}
```

When `conflicts` is true, `conflict_paths` lists paths with unmerged index entries.

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `branch` | string | Branch created and checked out. |
| `base` | string | Stash base commit (parent 1 of the W commit). |
| `oid` | string | Stash W commit that was applied. |
| `index` | number | Stash index when the commit was on the stack (omitted for an off-stack commit id). |
| `conflicts` | boolean | Whether merge conflicts remain after apply. |
| `conflict_paths` | array of strings | Conflicted paths when `conflicts` is true. |

### `grit stash clear`

Without `--force` (preview only):

```json
{
  "cleared": 2,
  "dry_run": true
}
```

After `grit stash clear --force`:

```json
{
  "cleared": 2,
  "dry_run": false
}
```

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `cleared` | number | Stash reflog entries removed or that would be removed. |
| `dry_run` | boolean | True when nothing was deleted (`--force` missing or `--dry-run`). |

### `grit stash apply` / `grit stash pop`

```json
{
  "oid": "d78432c4772cb8509513bca138a60d13e5ad50fd",
  "index": 0,
  "conflicts": false,
  "index_restored": false,
  "dropped": true
}
```

When `conflicts` is true, `conflict_paths` lists paths with unmerged index entries. On a clean apply or pop the field is omitted.

Pop adds `dropped` (whether the entry was removed):

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `oid` | string | Stash W commit id (always set on apply). |
| `index` | number | Stash index when the selector was on the stack (omitted for a bare commit id). |
| `conflicts` | boolean | Whether merge conflicts remain. |
| `index_restored` | boolean | Whether `--index` was used. |
| `conflict_paths` | array of strings | Paths with unmerged index entries when `conflicts` is true. |
| `dropped` | boolean | (`pop` only) Whether the stash entry was removed. |

### `grit stash drop`

```json
{
  "index": 0,
  "oid": "d78432c4772cb8509513bca138a60d13e5ad50fd"
}
```

| Field | Type | Meaning |
| ----- | ---- | ------- |
| `index` | number | Stash index removed. |
| `oid` | string | Full hex id of the dropped stash W commit (for recovery with `grit stash apply <oid>`). |

## Markdown output

Schema: [v1](https://grit-scm.com/docs/stash/schemas/stash/v1.json/index.md)
With `--markdown`, list/apply/pop/drop use bullet lists; `show` renders the diffstat or patch like other diff commands.



```text
## Stash

- Saved **On main: wip** · `690b7a6`
```

A conflicted pop lists the paths and the next steps:

```text
## Stash pop

- **stash@{0}** · stopped with conflicts (entry kept)
  - `f.txt`

→ resolve conflicts, `grit add PATH`, then `grit stash drop`
```

When the stash is empty, `pop` (and `apply` with no entries) reports a plain message instead of stash ref syntax:

```console
$ grit stash pop
error: no stash entries
hint: save local changes with `grit stash push`
```

## 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)). |
| `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.

With `--json`, typed stash failures use the shared [error schema](https://grit-scm.com/docs/stash/schemas/error/v1.json/index.md). Common `kind` values include `stash_not_found` when `apply`, `pop`, `drop`, or `show` names an index with no entry (for example `pop` on an empty stack), `not_a_repository` when the command needs a repository, and `lock_held` when another process holds `.git/index.lock` before any stash write. Human messages name the missing index (`stash@{n}`) or say `no stash entries` when the stack is empty. Apply and pop that would overwrite local tracked edits or clobber an on-disk untracked file exit **1** without changing the stash entry; `--json` still carries the error string (no separate `kind` yet for those cases).

When another process holds `.git/index.lock`, `grit stash`, `stash push`, `apply`, `pop` and `branch` fail with exit `1` (`kind` `lock_held` under `--json`) before writing anything: no stash commit or `refs/stash` entry is created, and the worktree and index are unchanged. Retry once the other process finishes. If a push fails after `refs/stash` was updated (for example an I/O error while resetting the worktree), the entry stays on the stash so the saved changes are never only a dangling object.


## See also

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