Git
version control
Git branches
Git tutorials
software development

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:

bash
git switch feature/login

To create a new branch and switch to it immediately:

bash
git switch -c feature/login

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.

bash
git checkout feature/login
git checkout -b feature/login

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.

bash
git branch

If you also want to see remote-tracking branches:

bash
git branch -a

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.

bash
git switch --track origin/feature/login

In many repositories, a shorter form also works if the branch name is unique:

bash
git switch feature/login

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:

bash
git status

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:

  1. Commit the changes.
  2. Stash the changes.
  3. Discard the changes intentionally.

A common stash flow is:

bash
git stash push -m "wip before switching branches"
git switch main

Later you can restore the work with:

bash
git stash pop

A Practical Workflow

A clean branch change usually looks like this:

bash
1git status
2git branch -a
3git switch main
4git pull --ff-only

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.

bash
git fetch --all --prune

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 checkout from old tutorials without realizing git switch is 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-name to move to an existing branch.
  • Use git switch -c branch-name to create and switch in one step.
  • Check git status before switching so local changes do not surprise you.
  • Use git branch -a and git fetch --all --prune when dealing with remote branches.
  • Treat branch-switch failures as working-tree-state problems first, not as command-syntax problems.

Course illustration
Course illustration

All Rights Reserved.