LinuxCommandLibrary

git-switch

Switch between Git branches

TLDR

Switch to an existing branch

$ git switch [branch_name]
copy

Create a new branch and switch to it
$ git switch [[-c|--create]] [branch_name]
copy

Create a new branch based on an existing commit and switch to it
$ git switch [[-c|--create]] [branch_name] [commit]
copy

Switch to the previous branch
$ git switch -
copy

Switch to a branch and update all submodules to match
$ git switch --recurse-submodules [branch_name]
copy

Switch to a branch and automatically merge the current branch and any uncommitted changes into it
$ git switch [[-m|--merge]] [branch_name]
copy

Switch to a tag
$ git switch [[-d|--detach]] [tag]
copy

SYNOPSIS

git switch [options] [branch]
git switch -c <new-branch> [<start-point>]
git switch --orphan <new-branch>
git switch -

PARAMETERS

<branch>
    The name of the branch to switch to. Can be an existing local or remote-tracking branch.

-c <new-branch>, --create <new-branch>
    Create a new branch named <new-branch> and switch to it. If <start-point> is given, it's created there, otherwise from HEAD.

-C <new-branch>, --force-create <new-branch>
    Like -c, but if <new-branch> already exists, it will be reset to <start-point> (or HEAD) and forcefully switched to.

-t, --track
    When creating a new branch, set up upstream configuration. This is implied if the argument is a remote-tracking branch.

--no-track
    Do not set up upstream configuration, even if the branch name matches a remote-tracking branch.

--orphan <new-branch>
    Create a new, unassociated orphan branch named <new-branch>, removing all previous history from the working tree.

-
    Switch to the previous branch (like git checkout -).

--detach
    Detach HEAD at the tip of the specified <branch> or commit. Allows working on a specific commit without being on a branch.

-f, --force
    Allow switching even if there are local modifications that would be overwritten. This will discard uncommitted changes if they conflict with the switch.

--discard-changes
    Alias for --force.

--guess, --no-guess
    Control if the branch name completion should be attempted based on remote-tracking branches. --guess is the default behavior.

DESCRIPTION

git switch is a command introduced in Git version 2.23, designed to provide a dedicated and safer way to switch branches or create new ones.

Historically, the git checkout command was overloaded, performing actions like switching branches, creating new branches, and restoring files. This often led to confusion. git switch aims to resolve this by separating the concerns, focusing solely on branch manipulation. It offers clearer semantics for switching between existing branches, creating new branches from a starting point, and detaching HEAD to a specific commit.

Unlike git checkout, git switch is more opinionated about state changes, explicitly warning or preventing operations that would discard local modifications, making it generally safer for new users. It works with both local and remote-tracking branches, providing options for tracking upstream branches automatically.

CAVEATS

git switch strictly handles branch operations and does not manage file-level restoration; use git restore for that.

While generally safer than git checkout, the --force option can still discard local, uncommitted changes. Always be mindful when using it.

This command was introduced in Git 2.23; it may not be available on older Git installations.

DEFAULT BEHAVIOR

If no branch name is specified, git switch will not perform any action and simply print usage information. Unlike git checkout, it does not list existing branches when called without arguments.

SAFETY FEATURES

git switch will prevent you from switching branches if you have uncommitted changes that would be lost by the switch, unless you explicitly use the --force (or --discard-changes) option to indicate that you want to discard them.

HISTORY

git switch was introduced in Git 2.23 (August 2019) alongside git restore. The primary motivation was to de-overload the widely used git checkout command, which historically performed both branch switching and file restoration. By separating these concerns, the Git command-line interface became more intuitive and less error-prone, improving clarity and user experience, especially for new users learning Git.

SEE ALSO

git checkout(1), git restore(1), git branch(1)

Copied to clipboard