git switch
git switch
: switch between branches.
The git switch
command allows you to navigate between branches in your repository. When you switch to a branch, Git updates your working directory to match the state of that branch, allowing you to work on a different version of your project without affecting other branches.
With the -c
or --create
flag, you can create a new branch and switch to it in a single command.
git switch
is a relatively new command introduced in Git 2.23 to simplify the process of switching between branches. Before that, developers had to use the multipurpose git checkout
command for both switching branches and other tasks like discarding changes in the repository. This was a source of confusion for newcomers. The git switch
command simplifies the workflow and reduces the potential for mistakes by providing a dedicated command for branch switching.
Examples
Switch to an existing branch named login-feature
:
git switch login-feature
Create a new branch named login-form-bugfix
and switch to it:
git switch -c login-form-bugfix
Switch back to the previous branch:
git switch -
Switch to a specific commit, entering a detached HEAD state (meaning you're not on a branch):
git switch --detach a1b2c3d