- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git switch Tutorial
Introduces git switch as the dedicated branch-switching command and clarifies how it differs from checkout.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
The short version
git switch is the dedicated command for moving between branches or creating a new branch and entering it immediately.
Why switch exists
git checkout used to cover both branch switching and file restoration. Git later introduced switch and restore to make those two jobs easier to understand.
Common usage
Switch to an existing branch
git switch feature/login
Create and switch in one step
git switch -c feature/login
This is the clearer modern form of git checkout -b feature/login.
Jump back to the previous branch
git switch -
This is very handy when you alternate between two branches during review or debugging.
When Git refuses to switch
If local changes would be overwritten, Git aborts the operation as a safety feature. Your usual options are to commit, stash, or explicitly discard changes if that is really what you intend.
Detached HEAD is also possible
git switch --detach <commit>
That is valid for temporary inspection or experiments. If the resulting commits matter, create a branch right away so they are easy to keep.
What problem this command solves in a workflow
git switch is the dedicated command for moving between branches. It changes HEAD to point at a different branch ref and updates the working tree files to match that branch's content. By separating branch switching from file restoration (which git restore handles), it makes the intent unambiguous.
Typical use cases
- Switch to an existing branch with
git switch <branch>, updating the working tree to match that branch. - Create and switch to a new branch in one step with
git switch -c <branch>, the modern replacement forgit checkout -b. - Jump back to the previous branch with
git switch -, handy when alternating between two branches during review or debugging.
Diagram view
Special cases and boundaries
- If Git warns that switching would overwrite local changes, decide whether to commit, stash, or discard before continuing.
git switch -ccreates a new branch from the current HEAD and switches to it immediately.git switch --detach <commit>enters detached HEAD state for temporary inspection; create a branch right away if the resulting commits matter.- Unlike
git checkout,git switchcannot restore individual file paths — its sole responsibility is branch movement.