Terminal GuideTerminal Guide

git switch Command Guide

Switch branches

4 min readLast updated: January 19, 2026
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

git switch <branch>Switch to branch
git switch -Switch to previous
git switch -c <name>Create and switch

Create

-c <name>Create new branch
-C <name>Force create (reset)
--orphan <name>Create orphan branch

Options

--detachDetached HEAD mode
--trackTrack remote branch
--discard-changesDiscard local changes

Downloadable Image Preview

Failed to generate preview

Basic Usage

The git switch command (Git 2.23+) is a clearer alternative to git checkout for switching branches.

bash
# Switch to an existing branch
git switch main

# Switch to previous branch
git switch -

# Create and switch to new branch
git switch -c feature-x

Creating Branches

bash
# Create and switch to new branch
git switch -c feature/login

# Create from specific commit
git switch -c hotfix abc1234

# Create tracking a remote branch
git switch -c feature --track origin/feature

# Force create (reset if exists)
git switch -C feature-x

# Create orphan branch (no history)
git switch --orphan docs

Common Options

OptionDescription
-c <branch>Create a new branch and switch to it
-C <branch>Create or reset branch and switch
--detachSwitch to commit in detached HEAD
--trackSet up tracking for remote branch
--discard-changesDiscard local changes when switching

Switch vs Checkout

bash
# These are equivalent:
git checkout main
git switch main

git checkout -b feature
git switch -c feature

git checkout --track origin/feature
git switch --track origin/feature

# But switch won't restore files:
git checkout -- file.js   # Restore file
git switch -- file.js     # Error! Use git restore

Why Use switch?

  • • Clearer intent - only switches branches
  • • Safer - won't accidentally modify files
  • • Requires explicit --detach for detached HEAD
  • • Part of the modern Git workflow

Practical Examples

bash
# Daily workflow
git switch main
git pull
git switch -c feature/new-thing

# Toggle between branches
git switch feature
git switch -   # Back to main
git switch -   # Back to feature

# Track remote branch
git fetch origin
git switch -c feature --track origin/feature

# Discard changes and switch
git switch --discard-changes main

Frequently Asked Questions

What is the difference between git switch and git checkout?

git switch (Git 2.23+) only handles branches, making it safer and clearer. git checkout does both branches and files, which can be confusing. Use switch for branches, restore for files.

How do I switch and create a branch in one command?

Use "git switch -c <branch-name>" to create a new branch and switch to it in one command. This is equivalent to "git checkout -b <branch-name>".

What happens if I have uncommitted changes when switching?

Git will prevent switching if your changes would be overwritten. Either commit, stash, or use --discard-changes to discard them.

Can I use git switch to detach HEAD?

Yes, use "git switch --detach <commit>" to enter detached HEAD state. Unlike checkout, switch requires the explicit --detach flag.

Summary

git switch is the modern, clearer way to switch branches. Use it instead of git checkout for branch operations.

Quick Reference

  • git switch <branch> - Switch to branch
  • git switch -c <name> - Create and switch
  • git switch - - Previous branch
  • git switch --detach <commit> - Detached HEAD

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles