Git Checkout Remote Branch: Step-by-Step Guide

When we need to work on a branch on a remote repository, such as a colleague’s work or a new feature branch, we face the challenge of accessing and updating that branch locally.

Fortunately, Git simplifies this process by offering straightforward commands to create a local copy of the remote branch. This allows us to switch to the desired branch, contribute, and collaborate effectively with the team.

For a quick answer, to checkout a branch named <branch_name>, we run:

git fetch origin <branch_name> git checkout -b <branch_name> origin/<branch_name>

In this article, we explore these commands and learn how they work in greater detail.

The Two-Step Process: git fetch and git checkout

To start working on a remote branch locally, we need to first make sure that our local repository is up to date with the remote repository. Then, we can create a local copy and start working on that branch.

Updating the local repository

To update the local repository, we use the git fetch command. Its simplest form uses no arguments:

git fetch

When used without arguments, the git fetch command retrieves all branches from the default remote repository. While this includes the branch we want, it will also occupy more local disk space.

If disk space is a concern, we can specify the branch we want to fetch by passing it as a parameter to the command:

git fetch origin <branch_name>

Replace <branch_name> with the name of the desired branch. The origin parameter specifies the name of the remote repository. By default, when we clone a repository, the remote is named origin.

It’s important to note that updating the remote references with fetch won’t affect any of the local files or cause conflicts. Locally, there are two distinct entities:

  • The current working directory
  • The local repository

Our current changes and commits are located in the current working directory. These are the files we see when we open the repository directory on our computer. These don’t get updated with git fetch. Only the local repository is updated, which can be thought of as a hidden copy of the remote repository on our local machine.

Checking out a remote branch

With an up-to-date copy of the remote repository, we can now create a local copy of a remote branch using the git checkout command:

git checkout -b <branch_name> origin/<branch_name>

Replace <branch_name> with the name of the desired remote branch. The first parameter after -b is the name we want the local branch to have. Conventionally, we use the same name for both, but we could use any other name, provided no other local branch has that name:

git checkout -b <local_branch_name> origin/<branch_name>

After successfully executing this command, Git will switch the current working directory to this branch, and we’ll be ready to start working on it.

git checkout vs git switch

The git checkout command has existed since the inception of Git. Its scope extends beyond creating local copies of branches or changing from one branch to another. For instance, it can get specific files from the remote repository.

In recent versions of Git, a git switch command was created whose sole purpose is to create local copies of remote branches and switch between local branches.

I recommend using git switch instead of git checkout because the extended capabilities of git checkout can sometimes lead to errors.

To learn how to perform the same actions we learned in this article using git switch, you can read this tutorial: Git Switch Branch: A Guide With Practical Examples.

Conclusion

Creating a local copy of a remote Git branch locally is very important for collaboration. By using git fetch and git checkout, we can update our local repository with remote changes and switch to the desired branch.

While the classic git checkout provides robust functionality, the newer git switch command offers a streamlined alternative specifically for branch management, reducing the risk of errors.

Source:
https://www.datacamp.com/tutorial/git-checkout-remote-branch