How to Switch Branches in Git Using Git Switch

Learn how to switch branches in Git using the git switch command. This beginner friendly guide covers how to change branches, switch to remote branches, and use git switch effectively.

Introduction to Git Branches

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 allows developers to experiment, fix bugs, or develop features in isolated environments without disrupting the main codebase.

In this tutorial, you’ll get hands-on experience on how to use the git switch command to switch to a local and remote branch as well as switch back to the previous branch efficiently. We’ll also highlight its advantages over git checkout and provide tips for avoiding common pitfalls.

Difference Between Git Switch and Git Checkout

Switching branches is an essential skill for every Git user. To perform this operation, we can use the git switch command, which makes it easy for us to navigate between branches in a repository. Think of it as a ‘Back’ button on a web browser, instantly moving you to the page you were on previously.

The git switch command is a newer alternative to the git checkout command. the following are the differences between these two commands:

Basis Git Switch Git Checkout
Operations Used for switching between branches Used for switching between branches, restoring working tree files, undoing changes, and more
Risk Relatively safer as it is used for a single operation Relatively riskier as it is used for multiple operations

How to Switch a Branch in Git Using Git Switch

In this section, we’ll learn how to use git switch to switch between a local and a remote branch and back to the previous branch in Git.

To perform these operations, we first need a local copy of the Git repository. For this tutorial, you’ll clone the Codecademy Docs remote repository to your 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 all set for the demonstration of the different operations that we can carry out by utilizing the git switch command. Let’s first check out how to use this command to switch to a local branch in Git.

How to Switch to a Local Branch in Git Using Git Switch

To begin, use the git status command to check the branch that we’re currently on in the cloned remote 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 as follows:

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 switch 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 branch simultaneously using the git switch -c command. This saves time when setting up new branches for features or experiments:

git switch -c branch-name

For example, 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 switch to a remote branch in Git.

How to Switch to a Remote Branch in Git Using Git Switch

A remote branch exists on a remote repository, such as one hosted on GitHub, 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 the following 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.

Fetch the remote branches to make them available locally using the following command:

git fetch

Then, let’s 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, let’s switch 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.

How to 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'

Concept Review and Next Steps

In this article, 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 as well. By mastering the git switch command, we can seamlessly navigate between branches in Git and manage our codebase more efficiently.

For a deeper dive into Git, check out the article How to Use Git for Beginners on Codecademy.

Author

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