How can I switch to another branch in Git?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Switching branches in Git changes the checked-out snapshot in your working tree from one line of development to another. The mechanics are simple, but branch switching becomes error-prone when local changes, untracked files, or missing remote branches are involved.
The Modern Command: git switch
For day-to-day work, the clearest command is git switch. It was added to separate branch changes from the many other jobs git checkout used to do.
To move to an existing local branch:
To create a new branch and switch to it immediately:
That is the most readable option for current Git usage because the command name says exactly what you are doing.
The Older Command: git checkout
Older tutorials and many existing scripts still use git checkout.
These commands still work, but git switch is easier to teach because it avoids overloading checkout with file restoration and branch movement in one interface.
Start by Listing Branches
If you are not sure what exists locally, inspect the branch list first.
If you also want to see remote-tracking branches:
That output helps you distinguish between a branch you already have locally and one that exists only on the remote.
Switching to a Remote Branch
If the branch exists on the remote but not yet locally, Git can create the local tracking branch for you.
In many repositories, a shorter form also works if the branch name is unique:
Git will infer that you mean origin/feature/login and create the tracking branch.
What Happens to Local Changes
Branch switching is easiest when the working tree is clean. Check that first:
If you have uncommitted changes and Git thinks they would be overwritten by the target branch, it blocks the switch. At that point you have three safe options:
- Commit the changes.
- Stash the changes.
- Discard the changes intentionally.
A common stash flow is:
Later you can restore the work with:
A Practical Workflow
A clean branch change usually looks like this:
That sequence checks whether the tree is safe to move, confirms available branches, switches to the target, and then updates it without creating an unnecessary merge commit.
When Branch Switching Fails
Most branch-switch problems are not about Git syntax. They come from repository state.
If Git says the branch does not exist, verify the exact name and fetch remotes.
If Git says files would be overwritten, inspect git status and resolve the local changes before trying again. Forcing past those warnings without understanding them is how people lose work.
Common Pitfalls
- Using
git checkoutfrom old tutorials without realizinggit switchis clearer for branch changes. - Trying to switch with uncommitted work that conflicts with the target branch.
- Forgetting to fetch remote references before assuming a branch is missing.
- Creating a new branch accidentally when the intention was to switch to an existing one.
- Ignoring
git status, which usually explains why Git refuses the change.
Summary
- Use
git switch branch-nameto move to an existing branch. - Use
git switch -c branch-nameto create and switch in one step. - Check
git statusbefore switching so local changes do not surprise you. - Use
git branch -aandgit fetch --all --prunewhen dealing with remote branches. - Treat branch-switch failures as working-tree-state problems first, not as command-syntax problems.

