Signup/Sign In

How to Check Out a Remote Git Branch?

We work with remote repositories to collaborate with other developers on our team. All of the developers will be contributing to the project and will be pushing branches to the remote repository. We can view and make changes to these branches by working on them on our local repository. In this tutorial, we will learn how to check out a remote Git branch on our local repository.

Checking out remote branches

Checking Out Remote Branches

In Git terminology, checkout means to navigate or switch to a different branch. Git provides us with the powerful and versatile Git Checkout command that is capable of doing this. To check out a remote branch we first need to fetch it from the remote repository and then create a new local branch based on the one that we just fetched. Follow the steps below to check out a remote branch.

  • Use the Git Fetch command to fetch the remote branch that you want to checkout. We can either mention the name of the remote branch or fetch all the remote branches present in the remote repository.
$ git fetch <remote> <branch-name>
$ git fetch <remote>
  • Use the Git Branch command with the -r flag to view the remote-tracking branch names. We can directly switch to these remote-tracking branches, but we won't be able to make any changes to it as our repository will be in a detached HEAD state.
$ git branch -r
  • We will use the Git Checkout command with the --track option to create a new local branch based on the remote-tracking branch that we fetched. This local branch will have the same name and the commit history as the remote branch. We can view these commits and even add new ones.
$ git checkout --track <remote>/<remote-branch>
  • Note that this local branch will have the remote branch as its Upstream. This will make it easier for us to push and pull changes between these branches. We will also know whether our local branch is leading ahead or lagging behind the remote branch.

Consider the following example to understand the above steps better.

  • Suppose, we want to check out a remote branch called the feature. First, we will use the Git Fetch command to fetch all the branches from the remote. Make sure you have set up the remote connection using the Git Remote Add command.

Fetching the remote branches using the Git Fetch command.

  • We can view the remote branches by running the Git Branch command with the -r flag.

Viewing the remote tracking branches by using the Git Branch command

  • Let's first try to check out the remote-tracking branch directly. As shown below, our HEAD gets detached and we cannot make new changes without creating a new branch. But we can view the commit history by using the Git Log command.

Directly checking out the remote tracking branches. This leads to a detached HEAD

Viewing the commits on the remote branch by using the Git Log command

  • Let's go back to our master branch and run the Git Checkout command with the --track option to create a new local branch based on the remote-tracking branch.

Using the --track option with Git checkout to create a new local branch based on the remote tracking branch.

  • We can also see that our local branch feature has an upstream origin/feature by running the Git Branch command with the -vv flag.

Viewing the upstream branches by using the -vv option with Git Branch.

Summary

Branches are a great way to work on features and try out new things. Collaborators will often share branches by pushing them to the remote repository. We can check out a remote branch by first fetching it to our local repository and then creating a new local branch based on it. By doing this we will be able to view the changes made by others and also modify them. This will also set up a tracking relationship between the local branch and the remote branch(upstream).



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight