Articles

How to Switch Branches in Git Using Git Switch (With Examples)

Learn how to switch branches in Git using the `git switch` command. Explore local and remote branches, common errors, and tips for smoother branch management.

Git is a robust version control system that allows developers to efficiently collaborate on projects, manage changes, and track different versions of their codebase. One of its standout features is branching, which enables developers to experiment, fix bugs, or develop features in isolated environments without disrupting the main codebase.

In this guide, we’ll learn how to use the git switch command to change to a local and remote branch as well as switch back to the previous branch in Git. We’ll also discuss the differences between git switch and git checkout, common errors, and best practices for using git switch efficiently.

Firstly, let’s understand what a branch is in Git and how it works.

What is a branch in Git?

A branch in Git is essentially a pointer to a specific snapshot of our project’s history. It allows us to diverge from the main line of development and keep working without affecting the main codebase. This makes branches an integral part of collaboration and experimentation in software development.

Imagine your project as a tree. The main trunk is your main or master branch, representing the stable version of your code. When you want to try a new feature, fix a bug, or work on a hotfix, you can create a new branch from the main one. This new branch lets you work in isolation while the main branch remains untouched.

Key characteristics:

  • Lightweight: Branches in Git are not full copies of our project but rather references to commits, making them fast and efficient.
  • Isolated: We can make changes on one branch without impacting others.
  • Flexible: We can switch between branches, merge them, or delete them when they are no longer needed.
  • Collaborative: Multiple team members can work on separate branches and merge their work later.

Now that we know how branches work in Git, let’s discuss how to switch to a local branch in Git using git switch.

Related Course

Learn Git: Introduction

Learn how to track changes in your code and switch between different versions with Git, an open-source version control system.Try it for free

Switch to a local branch in Git using git switch

Before we learn how to change branches using git switch, we need to have a local copy of the Git repository that we want to work on. For this tutorial, we’ll clone the Codecademy Docs remote repository to our local machine and work on it. Here, a remote repository refers to a repository that’s hosted on a Git hosting platform, such as GitHub, GitLab, or Bitbucket.

So, let’s clone the repository to our local machine using the git clone command:

git clone https://github.com/Codecademy/docs.git

Then, navigate to the cloned remote repository:

cd docs

Now, we’re ready for the git switch demonstration.

To begin with, use the git status command to check the current branch in the cloned repository:

git status

Here is the output:

On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

As we can see here, we’re currently on the main branch.

Now, let’s use the git branch command to create another branch named test1:

git branch test1

The syntax for switching branches is:

git switch branch-name

Here, branch-name is the name of the branch to which we intend to switch.

We will now use the git switch command to change to the branch test1 from branch main:

git switch test1
Switched to branch ‘test1'

We’ve now successfully switched to the branch test1.

Moreover, we can create and switch to a local branch simultaneously using the git switch -c command. This saves time when setting up new branches for features or experiments.

Here is the syntax for it:

git switch -c branch-name

Let’s use the git switch -c command to create and navigate to another branch test2 from branch test1:

git switch -c test2
Switched to a new branch 'test2'

As we can see, we’ve created and switched to a new branch test2.

In the next section, we’ll learn how to use the git switch command to change to a remote branch in Git.

Switch to a remote branch in Git using git switch

A remote branch exists on a remote repository, and we must fetch it to work with it locally. We can use the git switch command to fetch and switch to a remote branch in the local repository.

For example, if we want to switch to the cppentry remote branch in the Codecademy Docs remote repository using git switch, we need to use this command:

git switch cppentry

Here is the output:

Switched to a new branch 'cppentry'
branch 'cppentry' set up to track 'origin/cppentry'.

We’ve now created and switched to a new branch cppentry, which is set up to track the fetched cppentry remote branch from origin. Here, origin is an alternative name for the Codecademy Docs remote repository, which was set automatically when we first entered the cloned remote repository on our local machine.

Moreover, the local branch cppentry is tracking the remote branch origin/cppentry, which helps us pull and push changes from the local branch to the remote branch.

Alternatively, we can use the git switch -c command to simultaneously create and switch to a branch while explicitly linking it to a remote branch. The linking enables us to pull and push changes from the local branch to the linked remote branch. Unlike the git switch command, the git switch -c command requires manually fetching the remote branches first.

So, let’s fetch the remote branches to make them available locally using this command:

git fetch

Then, list the fetched remote branches using the git branch -r command:

git branch -r
origin/HEAD -> origin/main
origin/agraves.test-branch
origin/cppentry
origin/dotnet
...

Next, change to the remote branch origin/dotnet using the git switch -c command:

git switch -c dotnet origin/dotnet
Switched to a new branch 'dotnet'
branch 'dotnet' set up to track 'origin/dotnet'.

As we can see, dotnet is the name of the new branch that gets created and switched to and then set up to track the remote branch origin/dotnet.

Next, let’s understand how to switch back to the previous branch in Git using git switch.

Switch back to the previous branch in Git using git switch

We’ve seen how to switch to a local branch in Git using the git switch command. The same syntax can be utilized to switch back to the previous branch in Git as well.

To begin with, let’s first switch to the main branch:

git switch main
Switched to branch ‘main’

Then, let’s switch again to the test1 branch:

git switch test1
Switched to branch ‘test1’

Next, if we want to switch back to the main branch, we can use the same command we used earlier:

git switch main
Switched to branch ‘main’

However, there is an alternative way to switch back to the main branch. We can use the git switch - command to switch back to the previous branch, i.e., main in this case. This command is particularly useful when alternating between two branches frequently, as it eliminates the need to remember branch names.

Primarily, let’s again switch to the test1 branch:

git switch test1
Switched to branch ‘test1’

Now, let’s use the git switch - command to switch back to the main branch for another time:

git switch -
Switched to branch ‘main’

We’re done learning how to use git switch to change branches in Git. In the next section, let’s compare it to the git checkout command to understand how they differ in their functionalities.

Now that you’ve learned how to use git switch to change branches, let’s compare git switch with the traditional git checkout command.

Differences between git switch and git checkout

The git switch command is a newer alternative to the git checkout command. Let’s check out the differences between them:

Feature git switch git checkout
Purpose Switch branches only Multi-purpose: switch branches, restore files
Simplicity Simpler and more intuitive Can be confusing due to multiple functions
Syntax for Branch Switching git switch <branch> git checkout <branch>
Create and Switch Branch git switch -c <new-branch> git checkout -b <new-branch>
File Checkout Not supported git checkout <file>
Default Behavior Does not restore files or stages Can restore files, discard changes, etc.
Learning Curve Easier for new users Steeper due to multifunctionality
Recommended For Branch switching Legacy workflows or file restoration

With the differences covered, let’s discuss some common errors that we may face while using git switch.

Common errors while using git switch

Understanding these common git switch errors and learning how to fix them will help us change branches in a smooth manner.

fatal: invalid reference: branch-name

Cause: The specified branch does not exist.

Solution:

Make sure you typed the branch name correctly. You can list available branches with:

git branch

error: Your local changes to the following files would be overwritten by checkout

Cause: You have uncommitted changes that would be lost by switching branches.

Solution:

You can run these commands to stash or keep the changes, switch to another branch, and then pop or import the changes:

git stash
git switch other-branch
git stash pop

To discard the changes and switch to another branch:

git reset --hard
git switch other-branch

error: pathspec 'branch-name' did not match any file(s) known to git

Cause: The branch doesn’t exist, and you didn’t specify -c to create a new one.

Solution:

If you’re trying to create a new branch:

git switch -c branch-name

In the next section, we’ll discuss some best practices for using git switch.

Best practices for using git switch

Applying these best practices will help us switch branches efficiently:

  • Use descriptive branch names: When using -c to create a branch, choose meaningful names that reflect the feature or fix, e.g., git switch -c fix/login-bug.
  • Avoid using git switch on detached HEADs: Be cautious when switching to commits directly (e.g., git switch --detach <commit>), as it puts you in a detached HEAD state and changes won’t belong to a branch unless explicitly committed and assigned.
  • Use - to quickly switch between branches: git switch - toggles between your current and last checked-out branch — useful when alternating between two branches frequently.
  • Verify changes are committed or stashed before switching: Before switching branches, ensure you’ve committed or stashed changes to prevent merge conflicts or lost work.

Conclusion

In this tutorial, we discussed how to switch a branch in Git using the git switch command. We learned how to clone a remote repository, create a branch, switch to a local and remote branch and switch back to the previous branch. By mastering the git switch command, we can seamlessly navigate between branches and manage our codebase more efficiently.

If you want to learn more about Git branching and collaboration, check out the Learn Git: Branching and Collaboration course on Codecademy.

Frequently Asked Questions

1. What happens to my current changes when I use git switch?

If you have uncommitted changes that conflict with the target branch, Git will prevent you from performing git switch. You must either commit, stash, or discard the changes first.

2. Is git switch available in all Git versions?

No, git switch was added in Git 2.23. Older versions require git checkout.

3. How do I switch to a branch with the same name as a remote branch?

Use git switch -c <branch-name> --track origin/<branch-name> to create a local branch that tracks or points to the remote one.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team