How To Switch Branch on Git

February 3, 2020

Introduction

Git is an open-source application for tracking projects. Multiple users can work on the same project simultaneously. A project can be branched to evaluate features without affecting the base project. Branching also allows developers to work in a test environment that tracks progress and doesn’t affect production applications.

This guide shows you how to switch branches in your Git project.

tutorial on switching branches in git

Prerequisites

  • Git project
  • A Linux system with access to a terminal window/command line

Switch Branch With Git Checkout Command

Access the command line and use the checkout command to check out the branch you want to use:

git checkout [branch_label]

Replace [branch_label] with the actual name of the branch you want to access. For example:

git checkout bugfix224

You have successfully switched to the bugfix224 branch and can start working on it.

Create and Check Out a New Branch

If you need to open a new branch, use the checkout command and add the –b option. This command both creates and opens a new branch:

git checkout -b bugfix231

By default, the new branch is based on the last commit in the currently checked-out branch.

Command createsnew git branch and switches to it

If you want to use a different source, specify that source as follows:

git checkout -b (new_branch)(source_branch)

This example basses the bugfix231 branch off the bugfix230 branch.

git checkout -b bugfix231 bugfix230

The output confirms that a new branch has been added and that you have switched to it.

Confirmation that a new branch based off an existing one has been created

The checkout command can be used to create branches as well as individual files. This feature has led to unwanted results, especially when branches and files have similar names. The switch command was introduced in Git version 2.23 as an attempt to simplify the process.

Switch Branch With Git Switch Command

The switch command was introduced in Git 2.23 and subsequent releases. The principle command structure is the same as with the checkout command. Access the command line and use the switch command to mount the branch you want to use:

git switch [branch_label]

Replace [branch_label] with the actual name of the branch you want to switch to.

For example:

git switch bugfix224

You have successfully switched to the bugfix224 branch and can start working on it.

Conclusion

You now know how to switch branches in your Git project. You are free to make changes to the branch’s code, add and test new features, and work on fixing bugs and glitches. Next, you should learn is how to set up a Git upstream branch, how to change it and how to have an overview of which Git branch is tracking which upstream branch.

Feel free to experiment as Git keeps records of committed changes. You can retrace your steps in case testing does not go as planned.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Install Git on CentOS 8
January 14, 2020

This article is a step-by-step tutorial on how to install Git on CentOS 8. As a popular version control...
Read more
How to Install and Use Git on Windows
January 8, 2020

Git tracks source code changes during the software development process. It can help coordinate work among...
Read more
How To Rename a Local and Remote Git Branch
January 6, 2020

Git is a version control system that helps you control the stages of software development. It uses name...
Read more
How to Create a New Branch in Git
February 24, 2022

This article outlines the basic commands needed to create a Git branch. A Git branch allows you to work on...
Read more