Synopsis
git switch [-c <new-branch>] [--detach] <branch>
Description
The git switch command, introduced in Git 2.23, is a focused replacement for the branch-switching half of git checkout. It only switches branches (or creates and switches in one step). It refuses to operate on individual files, eliminating the most common source of checkout footguns. If you're learning Git today, prefer git switch for branches and git restore for files.
Like checkout, switch updates HEAD, the index, and the working tree to match the target branch. It will refuse to switch if doing so would clobber uncommitted changes, unless you pass --force.
In day-to-day use, git switch integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git switch --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git switch interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git switch's side effects helps avoid all three.
Common Options
| Option | Description |
|---|---|
-c <name>, --create | Create a new branch and switch to it. |
-C <name>, --force-create | Create or reset a branch and switch to it. |
--track | Set up tracking with a remote branch. |
--detach | Switch to a commit in detached HEAD mode. |
- | Switch to the previous branch (like cd -). |
--discard-changes | Throw away local changes when switching. |
-m, --merge | Three-way merge local changes when switching. |
Examples
git switch main
# Move to the main branch
git switch -c feature/search
# Create and switch to a new branch
git switch -
# Toggle back to the previous branch
git switch --detach v3.0.0
# Inspect a tag without creating a branch
Common Mistakes
A common confusion is expecting git switch to operate on files like git checkout does. It won't — use git restore instead. If you have uncommitted work that conflicts with the target branch, switching will fail; either commit, stash, or use --merge to attempt a three-way merge.
Related Commands
git checkout, git restore, git branch, git stash