How to Checkout Remote Branches in Git

Learn how to checkout remote branches in Git and manage remote repositories effectively. We'll cover essential commands like `git branch -r`, `git fetch`, `git pull`, `git checkout`, and `git switch` to help us work with remote branches.

Introduction to Remote Branches in Git

Git, a popular version control system, helps developers collaborate on projects seamlessly. With Git branches, developers can work on different features or bug fixes without interfering with the main codebase, which is present on the main branch. Branches serve as pointers to specific commits in a project’s history.

There are two types of Git branches, including local and remote. Local branches are created and managed on a local machine, allowing developers to work on different versions of code independently. Remote branches, on the other hand, are created on a remote repository and are shared with other developers. Here, remote repository indicates a repository that’s hosted on a Git hosting platform, such as GitHub, GitLab, or Bitbucket.

In this guide, we’ll navigate through the Git commands that help us to list, fetch, and check out remote branches, such as git branch -r, git fetch, git pull, git checkout, and git switch.

How to Clone a Remote Repository in Git

Before we start interacting with remote branches, we need to clone a remote repository to the local workspace, which we’ll work on. For this tutorial, you will be cloning the Codecademy Docs remote repository, i.e., making a local copy of the repository on our machine.

Start by opening a terminal on your computer (Ctrl + Alt + T on Linux or Ctrl + Alt + T on Mac). In the screen that shows up, we can write various Git commands to perform different Git operations.

Then, clone the Codecademy Docs remote repository to your local machine by writing the following command in the terminal:

git clone https://github.com/Codecademy/docs.git

The git clone command enables us to clone a remote repository to the local machine. In this case, the URL used above points to the Codecademy Docs remote repository, which is cloned to our local workspace.

After the operation is completed, navigate to the cloned repository using the cd command in the terminal:

cd docs

Since you’re now all set with a cloned remote repository in the local workspace, let’s discover some ways we can interact with remote branches in Git.

How to List Remote Branches in Git

To stay up to date with the latest changes in a remote repository, listing remote branches is essential. To list all the remote branches in a repository, we can use the git branch -r command.

If we dive deeper, the git branch command allows us to get a list of all the local branches in a repository. However, attaching the -r flag, short for --remotes, enables us to obtain a list of all the remote branches in the repository.

Since you’re already inside the cloned remote repository, list all the remote branches in the repository using the git branch -r command:

git branch -r
origin/HEAD -> origin/main
origin/main

The output, i.e., the lines that follow the git branch -r command indicate the remote branches that exist in the repository.

How to Fetch Remote Branches in Git

To check out the state of a remote branch in the local repository, fetching remote branches is crucial.

There are two commands that we can use to fetch a remote branch from a remote repository:

The git fetch command fetches a remote branch to the local repository without automatically merging it with the branch that we’re currently working on. On the other hand, the git pull command fetches a remote branch to the local repository and merges it with the working branch.

Before checking out an example for the git fetch command, look at the syntax for it using the following command:

git fetch repo-name branch-name

In the code above, repo-name indicates the remote repository from which the remote branch branch-name is to be fetched.

Here’s an example that uses the git fetch command to fetch the remote branch cppentry from origin, an alias or alternative name for the Codecademy Docs remote repository. This alias is automatically set when we enter the cloned remote repository for the first time:

git fetch origin cppentry
From https://github.com/Codecademy/docs
* branch cppentry -> FETCH_HEAD

Moreover, we can use the git fetch command to fetch all the remote branches from origin in one go:

git fetch origin

Let’s check out the list of fetched remote branches using the git branch -r command:

git branch -r
origin/HEAD -> origin/main
origin/agraves.test-branch
origin/cppentry
origin/dotnet
origin/full
...

The next command in the list is git pull. The following example uses this command to fetch the remote branch dotnet from origin:

git pull origin dotnet
From https://github.com/Codecademy/docs
* branch dotnet -> FETCH_HEAD
Auto-merging README.md
Merge made by the 'ort' strategy.
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

As we can see above, the git pull command merges the fetched remote branch with the working branch, whereas the git fetch command only fetches the remote branch without merging it with the working branch. Hence, if we want to review the state of a remote branch and keep the working branch intact, we can use git fetch. On the other hand, if we want to merge the latest changes from a remote branch to the working branch, we can use git pull.

Now, let’s see how to check out a remote branch in Git.

How to Check Out Remote Branches in Git

Checking out remote branches allows us to work on specific features or fixes that are available on a remote repository.

To check out a remote branch in a repository, we can use two commands:

The git checkout command lets us switch to a remote branch after fetching it. Alternatively, newer Git versions allow us to directly check remote branches using git switch.

Let’s look at an example for the git checkout command. To start, fetch the remote branch full from origin using the git fetch command:

git fetch origin full
From https://github.com/Codecademy/docs
* branch full -> FETCH_HEAD

Then, switch to the fetched remote branch full using the git checkout command:

git checkout full

Besides git checkout, we can also use the git checkout -b command, where we can create and checkout a branch at once:

git checkout -b full origin/full

Here, full is the name of the local branch that we’re creating and origin/full is the name of the remote branch. The name of the local branch can be the same or different from the remote branch.

The next command we can use is git switch, which is a newer alternative to git checkout. This command helps us fetch and switch to a remote branch. The difference between git switch and git checkout is that git checkout only switches to an already fetched remote branch. It doesn’t fetch a remote branch on its own. On the other hand, the git switch command both fetches and switches to a remote branch.

Next, the following example uses the git switch command to fetch the remote branch sqlterms from origin and switch to it:

git switch sqlterms

Apart from git switch, we also have the git switch -c command, which helps us create and switch to a branch at once, similar to git checkout -b. However, in this case, we need to fetch the remote branch first and then use the git switch -c command:

git fetch origin sqlterms
git switch -c sqlterms origin/sqlterms

Though the git checkout command is older, it’s still available for us to use. So, we can still use it whenever we feel right.

Review and Next Steps

In this article, we’ve covered the essential commands to interact with remote branches in Git. We not only learned how to list and fetch remote branches, but also understood how to checkout a remote branch, making ourselves ready to work with remote branches efficiently.

Mastering these Git commands is essential for collaborating on projects effectively. By understanding how to list and fetch remote branches as well as checkout a remote branch, we can streamline the development process and contribute to projects seamlessly.

To learn how to set up Git and GitHub, check out this article on Codecademy.

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team