How to Switch a Git Branch

How to Switch a Git Branch

A very basic guide on how to switch a branch in Git using Ubuntu 20.10, but for any operating system with the command line git tools installed.

Git is a distributed version control system. It supports non-linear, distributed development, allowing multiple contributors to work on a project simultaneously within what are called branches. This guide walks you through the basics of what a branch is and how to switch between branches.

What is a Branch in Git?

If you're doing work on a website, application, or other Git project, you probably don't want to push the code to production immediately. This is where creating a branch is necessary, as it marks what version of the code you branched from and allows you to work without breaking anything currently in production, or having you disturb other developers (or vice versa).

Why Would I Need to Switch Branches in Git?

Different branches have different purposes, so sometimes you need to switch from one to another. For example, you may be working in two branches yourself, or you may be pair programming. Git was designed to anticipate the need to switch  between branches with git checkout (git switch was introduced to be more intuitive and decrease the number of functions in git checkout, but it is currently considered experimental and not covered in this article).

Switching to an Existing Git Branch

You may have multiple tasks you're working on in separate branches. Let's say one is a hotfix and one is a feature request. You realize you have something to add on the hotfix, so you need to switch to that branch in Git:

Enter git checkout quick-hotfix at the command prompt.

You should then get a response stating:

Switched to branch 'quick-hotfix'

At that point, you can complete the hotfix.

Switching to a New Git Branch

Suppose you are updating your product's API and creating the branch you need to switch to. To do so:

Enter a command similar to the following at the command prompt. The -b flag is followed by the name of the new branch.

 git checkout -b update-api

You should then get a response stating:

Switched to a new branch 'update-api'

You can then commit work to that branch.

Switching to a Remote Git Branch

Using remote branches is one of the best ways to work with your colleagues. Let's say you're pair programming for the day and your coworker has a branch called "edit-button-feature-request" ready for the two of you. To switch to a remote branch in Git:

Enter git checkout --track origin/edit-button-feature-request at the command prompt.

You should then get a response stating:

Branch edit-button-feature-request set up to track remote branch newsletter from origin.
Switched to a new branch 'edit-button-feature-request'

At that point, a new local branch is created, allowing you to start on that feature request, and push to that branch as necessary.


If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.