# Global options

> Options that apply to every grit command.

These options work on any command (and on `grit` itself when you pass no subcommand).

| Option | Description |
| --- | --- |
| `--json` | Print one JSON object instead of human-readable text. Errors become `{"error": "..."}`. |
| `--markdown` | Print agent-friendly Markdown instead of human-readable text. Most commands render structured headings, lists, and tables (or fenced diffs) rather than dumping JSON. Mutually exclusive with `--json`. |
| `--filter <EXPR>` | Apply a jq expression to the JSON output. Requires `--json`. Iterator-style filters always emit a JSON array; singular filters emit one value (`null` when absent). |
| `--threads <N>` | Worker threads for parallel library work inside this process. `0` selects all logical CPUs (Git-style auto); `1` forces serial paths. Does not change human, `--json`, or `--markdown` output shape. |
| `--no-optional-locks` | Skip opportunistic index updates that take optional locks (same as `GIT_OPTIONAL_LOCKS=0`). Stat refresh still runs in memory; `.git/index` is left untouched. |
| `-h`, `--help` | Print help for `grit` or for a command. |
| `-v`, `--version` | Print the version of `grit`. `-V` works too. |

Running `grit` with no command is the same as [`grit status`](https://grit-scm.com/docs/status/index.md).

## Repository and config options

These go **before** the command name (`grit -C repo status`, not `grit status -C repo`), as in Git, so they never collide with a command's own short flags.

| Option | Description |
| --- | --- |
| `-C <DIR>` | Run as if `grit` was started in `<DIR>`. Repeatable: each one is relative to the previous (`-C a -C b` is `a/b`). An empty `<DIR>` (`-C ""`) changes nothing. A directory that does not exist fails with exit `1`. |
| `-c <KEY=VALUE>` | Set a config value for this invocation only, without touching any config file. Repeatable; a later `-c` wins over an earlier one, over `GIT_CONFIG_COUNT` pairs and over every config file. A bare `<KEY>` with no `=` means `true`, and `<KEY>=` sets the empty string. An invalid key is a usage error (exit `2`). |
| `--git-dir <PATH>` | Use this repository directory instead of discovering one. Same as `GIT_DIR`. Without `--work-tree`, the current directory is the work tree (unless the repository is bare or sets `core.worktree`). |
| `--work-tree <PATH>` | Use this directory as the top of the work tree. Same as `GIT_WORK_TREE`. |

`-C` is applied first, so a relative `--git-dir` or `--work-tree` is resolved from the new directory. The overrides are exported to everything `grit` starts, using the same `GIT_DIR`, `GIT_WORK_TREE` and `GIT_CONFIG_PARAMETERS` encoding Git uses, so hooks that run `git` see the same repository and config.

```console
$ grit -C services/api status --json --filter .branch
"main"
$ grit -c user.name="CI Bot" -c user.email=ci@example.com commit -m "Update lockfile"
[main 3f9c2ab] Update lockfile
1 change committed
$ grit --git-dir /srv/site.git --work-tree /var/www/site status
```

On Unix, the final working directory after all `-C` options, and the paths supplied to `--git-dir` and `--work-tree`, must be valid UTF-8. Unsupported repository selections are rejected before discovery or command execution with exit **2**, so a raw-byte name cannot select a similarly named Unicode repository. Human output reports the usage error on stderr; `--json` writes an error object with `kind: "usage"` on stdout; `--markdown` writes `**Error:**` on stdout. Traversing a non-UTF-8 directory with `-C` is supported when a subsequent `-C` returns to a UTF-8 working directory.

Library embedders get the same behavior from [`Environment::with_git_dir`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.Environment.html), [`Environment::with_work_tree`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.Environment.html) and [`Environment::push_config_override`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.Environment.html) with a parsed [`ConfigOverride`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.ConfigOverride.html).

## Environment variables

The `grit` CLI snapshots the process environment into [`Environment`](https://grit-scm.com/docs/library/repository/index.md) when opening a repository. Library embedders should construct an [`Environment`](https://grit-scm.com/docs/library/repository/index.md) explicitly instead of relying on these variables.

| Variable | Effect |
| --- | --- |
| `GIT_DIR`, `GIT_WORK_TREE`, `GIT_CEILING_DIRECTORIES`, `GIT_PREFIX` | Repository discovery |
| `GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_ALTERNATE_OBJECT_DIRECTORIES` | Index and object store paths. With `GIT_OBJECT_DIRECTORY` set, commands that open the repository (including `fetch`, `pull` and `push`) read and write objects there instead of `.git/objects`, which stays visible only when listed in `GIT_ALTERNATE_OBJECT_DIRECTORIES`. The directory must exist; an empty value is an error, as in Git |
| `GIT_CONFIG`, `GIT_CONFIG_GLOBAL`, `GIT_CONFIG_SYSTEM`, `GIT_CONFIG_NOSYSTEM`, `GIT_CONFIG_COUNT`, `GIT_CONFIG_KEY_n`, `GIT_CONFIG_VALUE_n`, `GIT_CONFIG_PARAMETERS` | Config cascade |
| `HOME`, `XDG_CONFIG_HOME`, `USERPROFILE`, `USER`, `USERNAME` | Home and identity fallbacks |
| `GIT_AUTHOR_*`, `GIT_COMMITTER_*` | Author/committer name, email, and date overrides for new objects |
| `GIT_SSH`, `GIT_SSH_COMMAND` | SSH transport command (`GIT_SSH_COMMAND` overrides `core.sshCommand` and `GIT_SSH`; see Git precedence) |
| `GIT_HTTP_LOW_SPEED_LIMIT`, `GIT_HTTP_LOW_SPEED_TIME` | HTTP low-speed abort thresholds (override `http.lowSpeedLimit` / `http.lowSpeedTime` from config) |
| `http.timeout`, `http.lowSpeedLimit`, `http.lowSpeedTime` | Smart HTTP read timeouts in config (see [`fetch`](https://grit-scm.com/docs/fetch/index.md)): `http.timeout` caps the whole transfer (default 600s); low-speed settings abort when throughput stays below the limit for the configured window. Fetch and clone poll cancellation during these waits, not only between read chunks |
| `GIT_NOTES_REF`, `GIT_NOTES_DISPLAY_REF` | Notes ref selection |
| `GIT_ATTR_SOURCE` | Tree to read `.gitattributes` from |
| `GIT_NAMESPACE`, `GIT_REPLACE_REF_BASE`, `GIT_NO_REPLACE_OBJECTS` | Ref namespaces and replace refs |
| `GIT_OPTIONAL_LOCKS` | When set to `0`, `false`, `no`, or `off`, grit skips optional index writes during read-mostly commands such as `status` (same as `--no-optional-locks`). Unset means optional locks are allowed. |
| `GRIT_TRACE` | Grit operation spans to stderr (`1` / `true` / `yes`), or appended to a file when set to an absolute path; see [Debugging and tracing](https://grit-scm.com/docs/debugging/index.md) |
| `PATH` | Helper lookup (e.g. signing programs) |
| `TZ` | Local timezone for dates when no explicit offset is given |
| `GIT_EXEC_PATH`, `GIT_INSTALL_ROOT` | Git helper discovery (credentials) |

Grit does not read `GIT_TEST_*` variables from Git's test suite. Use repository config instead (`pack.writeReverseIndex`, `index.version`, …), [`InitOptions::with_init_filesystem`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.InitOptions.html#method.with_init_filesystem) with [`PrecomposeDetection::Always`](https://docs.rs/grit-lib/latest/grit_lib/config/init_filesystem/enum.PrecomposeDetection.html#variant.Always) for NFD/NFC init probes, and [`RepositoryOptions::with_reftable_write`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.RepositoryOptions.html#method.with_reftable_write) to disable reftable auto-compaction in tests. For `safe.directory` behavior in tests, implement [`OwnershipCheck`](https://docs.rs/grit-lib/latest/grit_lib/ownership_check/trait.OwnershipCheck.html) and pass it through [`RepositoryOptions::with_ownership_check`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.RepositoryOptions.html#method.with_ownership_check) (keep `GIT_TEST_ASSUME_DIFFERENT_OWNER` only when invoking the system `git` binary for cross-checks). `GIT_INDEX_VERSION` and `GIT_PRINT_SHA1_ELLIPSIS` are also ignored.
