Git: How to check out a new remote branch

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Working with remote branches is a vital part of any collaborative development process using Git. Branches represent independent lines of development, which can be created, worked on, and eventually merged back into the main codebase. When a team member creates a new branch and pushes it to a remote repository, you may need to check out this branch to collaborate or review changes. This tutorial will guide you through the steps required to accomplish this, with code examples illustrating each step.

Prerequisites

Before we go into the details of checking out a remote branch, ensure that you have the following prerequisites in place:

  • Git installed on your system
  • Basic understanding of Git commands
  • Access to a remote Git repository (such as one hosted on GitHub, GitLab, or Bitbucket)

Step-by-Step Instructions

Step 1: Clone the Remote Repository

Firstly, if you have not already done so, you need to clone the remote repository to your local machine. Use the following command to clone a repository using its URL:

git clone https://github.com/username/repository.git

Step 2: Fetch the Latest Updates

Fetching the latest changes from the remote repository ensures that you have up-to-date information about all the branches that exist. It does not merge the changes into your current branch but updates your remote-tracking branches.

git fetch origin

Step 3: List All Branches

After fetching the updates, you can list all available branches by running the following command. It will show you both local and remote branches.

git branch -a

Step 4: Check Out the Remote Branch

To check out a remote branch that you don’t have locally, use the git checkout command followed by the branch name, with the origin/ prefix to denote that it is a remote branch:

git checkout -b new-branch origin/new-branch

This command does two things: it creates a new local branch (new-branch) based on the remote branch and switches you to it. If a branch with the same name already exists in your local branches, Git will prompt that.

Alternative: The Git ‘switch’ Command

Starting with Git version 2.23, you can also use the git switch command to check out a branch:

git switch -c new-branch --track origin/new-branch

This is a more explicit and easier-to-understand command for creating and switching to new branches. The --track option tells Git to set up a tracking relationship between the local and remote branch.

Step 5: The Shortcut: Checkout with ‘–track’

Git provides a shortcut to automatically set up a local branch that tracks a remote branch, assuming the local branch does not exist:

git checkout --track origin/new-branch

This command will set up ‘new-branch’ in your local repository to track the ‘origin/new-branch’ from the remote repository and switch to it.

Dealing with Name Conflicts

Sometimes, you may face conflicts if there is a local branch with the same name as the remote branch. In this case, Git will not allow you to checkout the remote branch directly and you will have to rename either one of the branches before proceeding. The following command would allow you to checkout a remote branch while creating a new local branch with a different name:

git checkout -b new-local-branch-name origin/new-branch

Advanced: Checking Out by SHA

For advanced users, it’s possible to checkout a remote branch at a specific commit by using a SHA hash:

git fetch origin

git checkout -b new-branch SHA_hash

Replace ‘SHA_hash’ with the commit’s SHA hash value. This operation is generally used for code examination or debugging at a certain point in history.

Best Practices

While checking out remote branches is relatively straightforward, here are some best practices you should follow:

  • Always perform a git fetch before attempting to checkout a new remote branch to make sure you have the latest data.
  • Use descriptive branch names to avoid conflicts and confusion.
  • Before checking out a new branch, ensure your current branch has no uncommitted changes that you might carry over.
  • If several users are collaborating on the same branch, communicate frequently to avoid conflicts.

Conclusion

Checking out a remote branch in Git allows you to work on different aspects of your project in parallel with your team. By following the commands and best practices outlined in this tutorial, you can seamlessly manage and switch between branches in your local and remote repositories.