Command Reference

git switch Tutorial

Introduces git switch as the dedicated branch-switching command and clarifies how it differs from checkout.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • Using local cleanup commands on already shared history
  • Continuing to rewrite before confirming a recovery path

Citations & Further Reading

  1. Git switch [Official]
  2. Git Branching Basic Branching and Merging [Book]

What you will learn

  • Understand the core purpose of git switch Tutorial
  • Master the basic usage and common options of git switch Tutorial
  • Introduces git switch as the dedicated branch-switching command and clarifies how it differs from checkout.
  • Understand key concepts: Why switch exists
  • Know when to use this feature and when to avoid it

Start with a problem

You're working in a Git repository and need to perform a specific task — but you're not sure which command or option is the right fit, or what this command can and cannot do.

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 for git checkout -b.
  • Jump back to the previous branch with git switch -, handy when alternating between two branches during review or debugging.

Diagram view

Branch switching flowswitch changes HEAD to point at a different branch and updates working tree files to match the target branch's content.
Input
Target branch nameCurrent working tree stateCurrent HEAD
Output
HEAD pointing at new branchUpdated working tree filesPrevious branch preserved
switch only handles branch movement. For file restoration, use git restore instead.

Special cases and boundaries

  • If Git warns that switching would overwrite local changes, decide whether to commit, stash, or discard before continuing.
  • git switch -c creates 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 switch cannot restore individual file paths — its sole responsibility is branch movement.

Try it yourself

  1. Practice the git-switch command in a test repository and observe state changes before and after
  2. Experiment with different options and compare the output differences
  3. Simulate a real scenario where you would need to use this, and walk through the full process

Further reading

Keep going on the same topic: