Introduction

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

A developer can switch branches to view and evaluate other solutions to a problem, work on different projects throughout the day, or review progress made by other developers.

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

Checkout Command to Switch Branch

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.

Git switch Command (Git 2.23)

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.

List Available Branches

You can list available Git branches by using the following command:

git branch

The system lists the *master branch, along with the names of any sub-branches.

git branch command lists all the branches in a git repository

Conclusion

You now know how to list and change branches in your Git project. You are free to make changes to the code of the branch, add and test new features, and work on fixing bugs and glitches.

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


Next you should also read