Git Cheat Sheet · Branches
Switch branches
git switch <branch>
git switch <branch> moves you to another branch: the files in the working copy take on that branch's state. If you have uncommitted changes that conflict, Git refuses: commit them or set them aside with git stash.
What it does
Puts the working copy in the state of another branch, which becomes the current branch.
A use case
You need to fix an urgent bug on main while working on a feature.
On the command line
git switch <branch>- Switches to a local branch.
git switch -- Goes back to the previous branch.
git switch <remote-branch>- Creates a local branch that tracks
origin/<branch>if it does not exist yet. git checkout <branch>- Older syntax, equivalent.
git branch -a- Lists local and remote branches.
In Merathon
- Double-click the branch in "Local" (left panel), or right-click → "Checkout".
- For a remote branch: right-click → "Checkout (local tracking branch)".
Frequently asked questions
What is the difference between git switch and git checkout?
git checkout does two jobs: switching branches and restoring files. Since Git 2.23, git switch and git restore separate them, which avoids mistakes.
How do I switch branches without losing my changes?
git stash, then git switch <branch>. When you come back, git stash pop.