content/snippets/git/s/switch-to-branch.md
Git branches are used to develop features, fix bugs, and experiment with new ideas. You can easily switch between branches using the git checkout command.
[!TIP]
You can learn all about using
git checkoutto create a new branch in the previous article about branch creation.
In order to switch to an existing branch, you can use git checkout <branch>. If you're using a newer version of Git, you can also use git switch <branch>.
# Syntax: git checkout <branch>
# or: git switch <branch>
git checkout patch-1
# Switches to the branch named `patch-1`
git switch patch-1
# Switches to the branch named `patch-1`
Subsequently, you can switch back to the previous branch using git checkout - or git switch -. Here, - is a shorthand for the previous branch.
# Syntax: git checkout -
# or: git switch -
git checkout patch-1
git checkout master
git checkout -
# Switches to `patch-1`
git switch patch-1
git switch master
git switch -
# Switches to `patch-1`