git-switch
Switch between Git branches
TLDR
Switch to an existing branch
Create a new branch and switch to it
Create a new branch based on an existing commit and switch to it
Switch to the previous branch
Switch to a branch and update all submodules to match
Switch to a branch and automatically merge the current branch and any uncommitted changes into it
Switch to a tag
SYNOPSIS
git switch [options] [<branch>]
git switch --detach [<commit>]
PARAMETERS
-c
Create new branch pointing to current HEAD
-C
Force create or reset branch to current HEAD
--detach, -d [<commit>]
Switch to commit in detached HEAD state
--discard-changes
Discard local changes when switching
--force, -f
Force overwrite local changes
-m, --merge-or-continue
Continue switch despite merge conflicts
--merge, -m
Abort if merge would overwrite uncommitted changes
-q, --quiet
Suppress output messages
--recurse-submodules
Update submodules to match target
--no-recurse-submodules
Do not update submodules
-P, --track
Set upstream for new branch
DESCRIPTION
The git switch command switches branches and restores working tree files, serving as a safer alternative to git checkout for branch switching. It refuses to overwrite uncommitted changes unless explicitly allowed, preventing accidental data loss. Use it to move between branches, create new ones from the current HEAD, or switch to a specific commit in detached HEAD state.
Key features include creating branches with -c or force-creating/resetting with -C. It supports discarding local changes with --discard-changes and handles merge conflicts during switches with -m. Unlike git checkout, it does not support checking out specific paths; use git restore for that.
This command promotes safer workflows by separating branch switching from file restoration, reducing errors in daily Git usage. Ideal for developers managing multiple branches without risking uncommitted work.
CAVEATS
Does not support path checkout; use git restore. May fail on uncommitted changes unless forced. Limited submodule handling by default.
COMMON USAGE
git switch main — switch to main branch.
git switch -c feature-branch — create and switch to new branch.
DETACHED HEAD WARNING
Switching to a commit with --detach leaves HEAD detached; create a branch to commit safely.
HISTORY
Introduced in Git 2.23.0 (August 2019) as a safer branch-switching porcelain command. Developed to split git checkout's dual role, pairing with git restore for file operations. Evolved in later versions with improved merge handling and submodule support.
SEE ALSO
git checkout(1), git branch(1), git restore(1), git status(1)


