# Agent guide

> Run grit from scripts and autonomous agents — JSON output, exit codes, non-interactive use, credentials, and where to read the docs.

This page is for programs and agents that drive [`grit`](https://grit-scm.com/docs/install/index.md) in automation. It describes **existing** CLI behavior only. Machine-readable **v1 JSON Schemas** for each command's `--json` success object are published at `/schemas/<command>/v1.json` (see the [JSON schemas](https://grit-scm.com/docs/schemas/index.md) index). Field-level tables on each command page remain the human-oriented reference; this guide covers the shared contract. Cross-command flag rules (limits, dry-run, messages, pathspecs) are summarized in [CLI conventions](https://grit-scm.com/docs/cli-conventions/index.md).

Examples below were captured on 2026-10-08 with `grit` built from this repository (`cargo build --release -p grit-cli`), in a temporary repository on branch `main`, unless noted.

## Prefer `--json` (and `--filter`)

Pass **`--json`** on every command when something other than a human reads the result. In that mode:

- **stdout** carries exactly **one** JSON value: the command's outcome object on success, or `{"error": "…"}` on failure.
- **stderr** carries progress lines, auth prompts, and human-style `error: …` diagnostics. Parsers should read **stdout only** for the outcome.
- The process still exits non-zero on failure, so you can branch on the exit code **or** the presence of an `error` key.

Use **`--filter`** with a [jq](https://jqlang.org)-like expression to narrow the JSON without piping through `jq`. It requires `--json`. Iterator-style filters (for example `.commits[].oid` or `.staged[].path`) always emit a JSON array, including `[]` when nothing matches. Singular filters such as `.branch` or `{branch, clean}` emit one value; a missing field is `null`.

```console
$ grit status --json --filter '{branch, clean}'
{
  "branch": "main",
  "clean": false
}

$ grit log --json --filter '.commits[].subject'
[
  "Say hi",
  "Start the project"
]
```

Plain `grit` with no subcommand is the same as [`grit status`](https://grit-scm.com/docs/status/index.md); `--json` works there too:

```console
$ grit --json --filter '{branch, clean}'
{
  "branch": "main",
  "clean": false
}
```

See [Scripting with grit](https://grit-scm.com/docs/scripting/index.md) for more `--filter` patterns. Per-command keys are documented under [The grit CLI](https://grit-scm.com/docs/global-options/index.md) command pages.

## JSON schema versioning

Each command path has versioned schema files under `content/schemas/`: `<command>.vN.json` (dotted paths for subcommands, such as `stash.list.v1.json`). The live contract is whatever **`current`** version the per-command table in `grit-cli/src/json_schema.rs` lists; older **`frozen`** versions stay on disk at stable `$id` URLs and are never regenerated.

**Additive on the live version (no bump):** new optional properties, new enum variants, new `definitions`, adding an `enum` to a former free-form string (for example structured `kind`), or widening nullability.

**Breaking (requires publishing vN+1):** property removed or renamed, JSON type narrowed, a property newly required, enum variant removed, `additionalProperties: false` where extra keys were previously allowed, or nullability removed from a field.

CI runs three gates:

- `json_schemas_are_current` — generated live schemas match committed bytes (or run update mode below).
- `json_schema_backward_compatible` — every committed schema file (90 total) stays backward compatible: live files vs freshly generated types; frozen older files are checked against themselves so they never drift silently.
- `json_snapshots_match_schemas` — golden `--json` snapshots validate against the matching schema.

Regenerate live schemas after intentional **additive** contract changes:

```bash
GRIT_UPDATE_SCHEMAS=1 cargo test json_schemas_are_current -p grit-cli -- --nocapture
```

Update mode refuses to overwrite the live file when the diff would be **breaking**; the test output lists each breaking JSON path and tells you to publish `vN+1` (add `<command>.v{N+1}.json`, bump `current`, append `N` to `frozen`). Frozen files are never touched by update mode.

## Error object shape

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

On failure with `--json`, stdout is a single object with an **`error`** string (the same message a human sees after `error:` on stderr). Some failures add a **`kind`** and optional fields (for example merge/pick **conflicts**, **`would_overwrite`** with `reason` and `paths` on switch refusal, **`lock_held`** with a **`lock`** object (`path`, and `holder.pid` / `holder.running` when recorded) when an index or low-level loose-ref write encounters a held lock, or **`hash_collision`** with an **`oid`** digest when object ingestion detects an SHA-1 chosen-prefix collision); see the error schema for the plain vs structured union. When grit knows the next step, a **`hint`** names the command to run (`grit remote add origin <url>`, `grit push -u origin main`, `grit auth`, `grit merge --continue`, …), and **`paths`** lists the files an `unmerged_paths` or `dirty_worktree` failure is about. Branch on `kind`, not on the `error` text.

```console
$ grit commit --json
{"error":"provide a commit message, e.g. grit commit \"what changed\""}
```

If `--filter` is set, it runs on `{"error": "…"}` like any other JSON value. A bad filter expression also yields `{"error": "…"}` on stdout:

```console
$ grit status --json --filter 'invalid!!!'
{"error":"invalid filter \"invalid!!!\": [(File { code: \"invalid!!!\", path: () }, Parse([(Nothing, \"!!!\")]))]"}
```

In **human** mode, errors print to **stderr** and stdout stays empty:

```console
$ grit commit
error: provide a commit message, e.g. grit commit "what changed"
```

## Exit codes

| Code | Meaning |
| --- | --- |
| **0** | Success (including some jq filters that evaluate to `null` when a path is missing). |
| **1** | Command failed (missing repo, rejected push, validation error, and similar). With `--json`, stdout still contains `{"error": "…"}`. |
| **2** | Usage error (unknown option, invalid subcommand flags, bad `--hunks` ids, or `diff --exit-code` when diff fails). Clap still prints help to stderr. With **`--json`**, stdout is `{"error":"…"}` even for clap usage errors. Without `--json`, usage errors may have empty stdout. |
| **128** | Remote unreadable (`clone`, `fetch`, `pull`, `push`, `remote refs`; `kind: remote_unreadable`), bundle missing prerequisites, or `merge --ff-only` / `pull --ff-only` refused. |
| **129** | Invalid `--author` / `--grep` / pickaxe pattern (`kind`: `invalid_pattern`). |

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

```console
$ grit --nope
error: unexpected argument '--nope' found

Usage: grit [OPTIONS] [COMMAND]

For more information, try '--help'.
```

```console
$ grit --json --nope
{"error":"unexpected argument '--nope' found"}
```

```console
$ grit status --filter '.branch'
error: --filter requires --json
```

[`grit push`](https://grit-scm.com/docs/push/index.md) can exit **1** even after printing a full JSON outcome when a ref was rejected (same idea as Git: the outcome is reported, but the operation failed).

## Color and terminal detection

Human output uses ANSI color only when **stdout is a terminal**, the environment supports ANSI (`grit_lib::porcelain::terminal::ansi_supported()`), and **`NO_COLOR` is unset** ([no-color.org](https://no-color.org)). Piped or redirected stdout is plain text; set `NO_COLOR=1` to force plain text on a TTY.

JSON mode never depends on color; always pass `--json` for machines.

## Markdown output (`--markdown`)

When a human-readable transcript is easier to consume than raw JSON, pass **`--markdown`**. It is mutually exclusive with `--json`; use JSON for strict parsing and Markdown for agent-oriented prose.

Each command page documents real `--markdown` stdout under **Markdown output** and links the same v1 JSON Schema as the JSON section (for example `Schema: [v1](https://grit-scm.com/docs/agents/schemas/status/v1.json/index.md)`). Treat that schema as the stable field contract even when stdout is Markdown.

Grit formats `--markdown` consistently:

| Convention | Behavior |
| --- | --- |
| **Headings** | `##` titles introduce sections (status groups, file lists, subcommand results). |
| **Bullets** | `-` lines for commits, paths, and simple lists; short oids often appear in backticks. |
| **Tables** | Markdown tables for rows of refs, remotes, worktrees, and similar; `\|`, `\n`, and `\r` inside cells are escaped. |
| **Diff fences** | One fenced `diff` block per changed file when a command emits patches; fence backticks lengthen if the patch text contains ``` so the fence stays unambiguous. |

Do not combine `--markdown` with `--filter`. See [Scripting with grit](https://grit-scm.com/docs/scripting/index.md) for exit codes, filters, and the shared JSON error object.

## Stay non-interactive

`grit` does **not** open an external editor for commit messages. [`grit commit`](https://grit-scm.com/docs/commit/index.md) requires a message on the command line (positional or `-m`); there is no `$GIT_EDITOR` path.

Commands and flows that can **block on a person**:

| Area | Behavior | How to avoid blocking |
| --- | --- | --- |
| [`grit auth`](https://grit-scm.com/docs/auth/index.md) | OAuth device flow: instructions on stderr, then polls GitHub until you authorize in a browser. | Run `grit auth` once in an interactive session before automation. Store the token via `credential.helper` (see below). |
| [`grit fetch`](https://grit-scm.com/docs/fetch/index.md) / [`grit push`](https://grit-scm.com/docs/push/index.md) / [`grit pull`](https://grit-scm.com/docs/pull/index.md) | On HTTPS `github.com` auth failure, if **stdin is a TTY**, grit may ask `Sign in to GitHub now? [Y/n]` and run the device flow. | Use a stored token from `grit auth`, or ensure stdin is not a TTY so grit prints a hint and exits without prompting. |
| [`grit clone`](https://grit-scm.com/docs/clone/index.md) / [`grit fetch`](https://grit-scm.com/docs/fetch/index.md) / [`grit pull`](https://grit-scm.com/docs/pull/index.md) / [`grit push`](https://grit-scm.com/docs/push/index.md) over **SSH** | Grit runs `ssh` with protocol stdin/stdout piped but **stderr inherited**, so the SSH child can still talk to `/dev/tty` (host-key confirmation, passphrase prompts, `ssh-askpass`, and similar). `--json` does not disable this. | Preconfigure keys and known hosts; for unattended jobs set `GIT_SSH_COMMAND='ssh -o BatchMode=yes'` so SSH fails fast instead of prompting. Grit resolves the ssh command like Git: `GIT_SSH_COMMAND`, then `core.sshCommand` from config, then `GIT_SSH`, then `ssh` on `PATH`. |
| [`grit update`](https://grit-scm.com/docs/update/index.md) | Runs the release installer (`curl \| sh` on Unix). | Do not invoke in unattended jobs unless you intend to upgrade the binary. |

There is no global `--yes` flag. For commits, always pass `-m` / a message argument.

[`grit auth logout`](https://grit-scm.com/docs/auth/index.md) is non-interactive and reports whether a helper had a token to erase:

```console
$ grit auth logout --json
{
  "logged_out": false,
  "host": "github.com"
}
```

### Plumbing commands (not JSON)

These speak binary or line protocols on stdin/stdout and have **no** `--json` mode: [`grit manager`](https://grit-scm.com/docs/manager/index.md), [`grit upload-pack`](https://grit-scm.com/docs/upload-pack/index.md), [`grit receive-pack`](https://grit-scm.com/docs/receive-pack/index.md). Agents should not wrap them as JSON commands.

`-v` / `--version` (or `-V`) prints a version string and does not emit JSON even if `--json` is passed:

```console
$ grit --json --version
grit 0.5.1
```

## Credentials (HTTPS)

For `https://github.com` remotes, sign in once with [`grit auth`](https://grit-scm.com/docs/auth/index.md). Grit uses GitHub's device flow, then stores an access token through your configured **`credential.helper`** (same mechanism as Git). Override the OAuth app id with `GRIT_GITHUB_CLIENT_ID` or `grit config --global grit.githubClientId …`.

Sign out with `grit auth logout`. You need a credential helper configured (for example `grit config --global credential.helper store` on Linux — see the [`grit auth`](https://grit-scm.com/docs/auth/index.md) page).

The same helpers serve any HTTPS host, not only GitHub. For GitLab, Gitea, Bitbucket or any other host, save a user name and token once with `grit auth <url> --username <user> --token <token>`: it stores them with your credential helper (setting one up if needed) for that host, without contacting it. `grit auth <url>` without `--username` signs in only to GitHub and `grit serve` hosts, and says so for anything else. In CI, a host-level line such as `https://ci:TOKEN@git.example.com` in the store file (`~/.git-credentials`) authenticates `grit clone`, `fetch` and `push` for every repository on that host, exactly as it does for Git.

When no helper has the credentials, grit asks an **askpass** program for them, like Git: `GIT_ASKPASS`, then `core.askPass`, then `SSH_ASKPASS` (the first one set wins; an empty value turns askpass off). The program gets one prompt as its argument, `Username for 'https://git.example.com': ` and then `Password for 'https://ci@git.example.com': `, and prints the answer on stdout. That is how most CI systems inject a token:

```bash
cat > askpass.sh <<'SH'
#!/bin/sh
case "$1" in Username*) echo ci;; *) echo "$CI_TOKEN";; esac
SH
chmod +x askpass.sh
GIT_ASKPASS=$PWD/askpass.sh grit clone https://git.example.com/team/app.git
```

A credential an askpass program supplies is saved with your credential helper once the server accepts it. Grit never prompts on the terminal, even when one is attached: when neither a helper nor an askpass program has the credentials, the command fails with `kind` `auth_required`, and the hint names the helpers that were asked and `grit auth <url> --username … --token …`. Set `credential.interactive=never` to skip askpass as well. When the server refuses a stored credential (an expired or rotated token), the error is `auth_rejected` and the helper erases the entry, as Git does; store the new token the same way. A `user:password@` in the URL is sent as it is; if the server refuses it the error is also `auth_rejected`.

SSH remotes do not use the GitHub device-flow token; they can still block on SSH prompts (see the SSH row above).

## Documentation for agents

| Resource | URL / path | Use |
| --- | --- | --- |
| **Agent skill** | [`grit skill`](https://grit-scm.com/docs/skill/index.md) | A `SKILL.md` for coding agents, printed by the installed binary: `grit skill > .agents/skills/grit/SKILL.md`. Includes CI-tested **Recipes** (precise commit, merge conflicts, rebase + lease push, fetch PR, shallow clone) and links to [CLI conventions](https://grit-scm.com/docs/cli-conventions/index.md). |
| **llms.txt** | [grit-scm.com/llms.txt](https://grit-scm.com/llms.txt) | Curated index of every docs page (llmstxt.org format). |
| **llms-full.txt** | [grit-scm.com/llms-full.txt](https://grit-scm.com/llms-full.txt) | Full text of all docs Markdown twins in site order. |
| **Markdown twin** | Same URL as HTML but `index.md` instead of `index.html` (e.g. [status/index.md](https://grit-scm.com/docs/status/index.md)) | One page per command or guide; linked from HTML as "Markdown". |
| **Command JSON fields** | Under [The grit CLI](https://grit-scm.com/docs/global-options/index.md) | Each command page lists real `--json` examples and field tables. |
| **grit-lib API** | [docs.rs/grit-lib](https://docs.rs/grit-lib) | Rust library reference when embedding grit in your own binary. |
| **Library guide** | [Library guide](https://grit-scm.com/docs/library/index.md) | Narrative Rust workflows on top of `grit-lib`. |

[llms.txt](https://grit-scm.com/llms.txt), [llms-full.txt](https://grit-scm.com/llms-full.txt), the two one-file bundles ([grit-cli.md](https://grit-scm.com/docs/grit-cli.md) and [grit-lib.md](https://grit-scm.com/docs/grit-lib.md)) and every Markdown twin are built from the same sources as the HTML pages on each deploy, so they never drift from the docs.

## See also

- [Scripting with grit](https://grit-scm.com/docs/scripting/index.md) — `--json`, `--filter`, and exit codes in brief
- [Global options](https://grit-scm.com/docs/global-options/index.md) — flags shared by every command
- [Docs overview](https://grit-scm.com/docs/index.md) — how the site is organized
