How to Check Out a Remote Git Branch

Abdul Jabbar Feb 02, 2024
  1. List Git Available Branches
  2. Fetch All Git Remote Branches
  3. Pull Changes From a Git Remote Branch
How to Check Out a Remote Git Branch

This article will introduce how to check out the remote Git branch using the checkout command. The version control tool that permits you to support and manage variable versions of your applications among the team of members is the Git version control tool. When your app comes across a new update, Git prefers to return those changes to the previous version and merge both versions in a new one.

As we know that, in Git, we have branches allowing us to work in multiple environments with different users working simultaneously. In Git, we have a master branch that is the main branch that holds our secure code that gets placed when our app is almost ready for production. Also, when we want to update that app, we can add a few more commits to this branch. This method is ideal for minor commits, but for major ones, it’s not preferable. For the major commit, you can use the other branches too.

For creating and using a new branch, we use the following command:

# create a new branch
git branch branch-name
# change environment to the new branch
git checkout branch-name

In the above commands, we have made a new branch. We will check out the branch and then add new changes; after that, we will merge that with the master branch and push it in the latest remote branch. But imagine if the remote branch already existed, and we have to pull the branch and the related work and all of its changes to our local branch, then we will run git checkout remote branch.

In Git, Checkout can be explained as the act of switching among various versions of a target unit. The command used for checkout in Git is git checkout, which operates upon three well-defined units: files, commits, and branches. git checkout can also be used to view old commits, but the target for almost all of this document will be checkout operations on branches.

If we say another developer on the same repository makes a remote branch, we want to pull that branch created by the other developer. Here’s how we have to do the following:

List Git Available Branches

The result of the following command will display the list of branches available for checkout. For the remote branches, there will be a prefix used with its remotes/origin.

git branch -a

Fetch All Git Remote Branches

With the help of the following command, we will fetch remote branches from its repository. origin is the name of the remote branch that we’re targeting. If we have an upstream remote name, we can call it git fetch upstream.

git fetch origin

Pull Changes From a Git Remote Branch

As we know that we cannot make changes directly on a remote branch. So here we need a local working copy. With the help of remote branches, we will check out the branch that we are interested in; here is how you would do it:

git checkout -b test origin/test
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Checkout