Git & GitHub
/Beginner
git switch
Definition
A newer command introduced in Git 2.23 explicitly designed to switch branches, resolving the confusing dual-nature of `git checkout`.
Explain Like I'm New
The modern, safer version of `git checkout`. It does exactly one thing: it switches branches. It cannot accidentally delete your files.
Real World Example
You want to go to the `dev` branch. Instead of typing `git checkout dev`, you type `git switch dev`.
Common Use Cases
- •Switching branches safely
Terminal Output
bash / terminal
# Switch to an existing branch
$ git switch main
# CREATE and SWITCH to a new branch
$ git switch -c feature/payment-gateway
# Quickly switch back to the branch you were just on previously (like a TV remote 'Last Channel' button)
$ git switch -
Interview Questions
basic
- What is the `git switch` equivalent of `git checkout -b` (create and switch)?
intermediate
- If `git switch` is better, why do people still use `git checkout`?