Description
Switches to a local branch, creates a branch from a remote-tracking ref or start point, or checks out a commit in detached HEAD mode. Updates tracked files in the working tree to match the destination.
Local edits are allowed when they only touch paths that are the same on both sides of the switch, or when a changed path already matches the destination tree in the index. Grit still refuses when your changes would conflict with files that differ between branches, or when an untracked file would be overwritten.
After a clone --no-checkout, the first switch creates the index and materializes the entire destination tree. This works with clones created by grit or system Git, including switching to the current branch, detaching at HEAD, or creating a branch at a commit with the same tree. The previous HEAD is still used for reflogs and hooks. Untracked files at destination paths block the switch. An existing empty index continues to represent staged deletions and receives the normal local-change protection.
Use --dry-run to run the same overwrite checks and report how many paths would change without updating HEAD, the index, the worktree, or reflogs. Agents can preflight a switch with --json --dry-run before mutating the tree.
On the first switch after clone --no-checkout, the dry-run count includes every path to materialize, including files unchanged between the old and new commits.
A switch either completes or changes nothing. Pressing Ctrl+C, or sending SIGTERM or SIGHUP on Unix, while files are being checked out stops the switch with error: operation cancelled; HEAD, the index and the work tree are left exactly as they were, so grit status stays clean, no .git/index.lock is left behind and the same switch can simply be run again. If cancellation is already requested before the switch starts (or is observed while the plan is being built), grit returns cancelled without deleting work-tree paths. 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 or closing the console window skips rollback. In the rare case that a path cannot be put back (for example a directory that became read-only), the error has kind checkout_incomplete and lists those paths in paths; HEAD and the index still did not move.
Switching is refused while a merge, rebase, pick, revert or am is paused, whether grit or git started it: those operations finish by committing onto the current HEAD, so moving it would land their result on the wrong branch. When grit knows how the operation finishes or is cancelled (merge, rebase, pick, revert), the error names those commands. --dry-run is refused too. A bisect does not block a switch.
A local branch can only be checked out in one worktree at a time (including linked worktrees created with git worktree add). Switching to a branch that is already checked out elsewhere fails.
With -c, creates the branch at the current commit (or at <start-point> when you pass one), then switches to it. When <start-point> is a remote-tracking branch (for example origin/main), grit configures upstream tracking the same way as grit branch --track and git switch -c, unless you pass --no-track or branch.autoSetupMerge is false/simple.
When <branch> names a remote-tracking branch (for example origin/feature), Grit creates feature at the same commit and switches to it if no local branch exists yet. It also records upstream tracking in the repository config (branch.feature.remote and branch.feature.merge), the same keys git switch writes, so grit status, grit pull, and grit push use the matching remote branch.
--detach checks out <rev> without moving a branch tip. Use another branch or grit switch <branch> to reattach.
In a sparse checkout (git sparse-checkout), only paths inside the sparse patterns are written to the worktree. Paths outside them are recorded in the index with the skip-worktree bit, the same as git switch. The same applies to grit merge, grit pull and grit reset.
After a real switch (not --dry-run), grit runs the post-checkout hook with the previous HEAD commit, the new one, and 1, as Git does. The old id is all zeros when switching away from a branch with no commits. The hook also runs when you switch to the branch or commit you are already on. Its exit status never changes the result; a hook that cannot be started prints a hook_not_run warning. See grit hooks.
Leaving a detached HEAD that carries commits of its own (made with grit commit after --detach) would leave those commits on no branch, recoverable only through grit reflog until garbage collection removes them. Grit still switches, but warns on stderr with the commits it leaves behind (newest first, up to 20) and a grit branch <name> <oid> command that keeps them. A commit counts as left behind when no ref, the stash, another worktree's HEAD, or the switch destination reaches it, so grit switch -c <new> from the detached commit leaves nothing behind. Without a commit-graph the check ends by commit date like Git's rev-list, so heavy clock skew can make the count wrong (see grit reset). --dry-run reports the same list without switching. Leaving a branch never warns: the branch still holds its commits.
Updating the working tree uses parallel checkout when the change set is large enough; tune checkout.workers and checkout.thresholdForParallelism. On non-Windows hosts, global --threads can cap checkout workers for this run; on Windows, checkout stays serial until checkout.workers is set in config (see parallelism).
Options
| Option | Description |
|---|---|
<branch> |
Local branch to switch to, remote-tracking name to DWIM, or new branch name with -c. |
-, @{-1} |
Switch to the previous branch (from the HEAD reflog); fails when there is no prior checkout entry. |
<start-point> |
With -c, commit to create the branch from (defaults to HEAD). |
-c, --create |
Create the branch first, then switch to it. Fails if the branch already exists. |
-t, --track |
With -c, always configure upstream from the start point. |
--no-track |
With -c, do not configure upstream. |
--detach |
Check out a commit in detached HEAD mode instead of switching a branch. |
--dry-run |
Run overwrite checks and report how many paths would change; do not update refs, the index, or the worktree. |
JSON output
Pass --json for stable, scripting-friendly output:
| Field | Type | Meaning |
|---|---|---|
branch |
string or null | Short branch name HEAD is on after the switch; null when detached. |
created |
boolean | true when -c or remote DWIM created the branch. |
upstream |
string or null | When -c configured upstream tracking, the remote branch name (for example origin/main); omitted when none was set. |
detached |
boolean | true when HEAD is detached after the switch (branch is null). |
commit |
string or null | Full hex oid when detached is true; omitted when attached. |
dry_run |
boolean | true when --dry-run was used (omitted when false). |
changed_paths |
integer or null | With --dry-run, count of paths that would differ after checkout. |
hooks_run |
array of strings | Hooks that ran after the switch (post-checkout when installed); empty on --dry-run. |
stranded_commits |
array | Commits only the previous detached HEAD reached, newest first, at most 20. Each has oid, change_id and subject. Omitted when nothing is left behind. |
stranded_total |
integer | Advisory count of commits left behind (may exceed the length of stranded_commits). Without a commit-graph, severe clock skew can make this inaccurate; it never controls the switch. Omitted when zero. |
stranded_truncated |
boolean | true when stranded_commits was capped (omitted when false). |
path_collisions |
array of strings | Index paths that collided during checkout when tracked paths share one on-disk file (case or Unicode normalization). Omitted when empty. |
When core.ignorecase or core.precomposeunicode is set and the destination tree contains paths that collide on this filesystem (different ASCII case or NFC/NFD spellings mapping to one file), grit prints a warning on stderr in human mode listing every colliding index path, the same message as grit clone. Only one path from each group exists on disk; status and diff treat index rows whose blob does not match the shared dentry as modified.
On overwrite refusal with --json, stdout is a single object with kind would_overwrite and would_overwrite.reason (local_changes or untracked) plus a sorted paths array, and a hint, for example {"error":"you have uncommitted changes — commit them before switching","hint":"…","kind":"would_overwrite","would_overwrite":{"paths":["notes.txt"],"reason":"local_changes"}}. See the error schema.
A branch that does not exist is kind branch_not_found with a hint.
With --create, an existing name is kind branch_already_exists, an invalid name is branch_invalid_name, and an empty repository with no start point is branch_no_commits. A start point that does not resolve is revision_not_found; one that resolves to a non-commit object is a plain error string with no dedicated kind.
While a merge, rebase, pick or revert is paused, the refusal is {"error": "interactive rebase in progress", "kind": "operation_in_progress"} (the error names the paused operation).
Markdown output
Exit status
See Exit codes for shared rules and structured --json kind values.
When another process holds .git/index.lock, a switch that would update the worktree fails with exit 1 (kind lock_held under --json) before touching anything: the worktree, index, HEAD and branches are unchanged. --dry-run never takes the lock.