Skip to main content
HomeTutorialsGit

Git Checkout Remote Branch: Step-by-Step Guide

To git checkout a remote branch, you first need to fetch the latest changes from the remote repository, then you can checkout the remote branch locally using its full name (e.g., origin/branch-name).
Sep 18, 2024  · 4 min read

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.

FAQs

I get an error: "please commit your changes or stash them before you switch branches." How do I fix it?

Git prevents us from changing to another branch if the current branch has uncommitted changes. Resolving this issue involves either stashing our current changes or committing them. 

To commit them we use the the following command:

git commit -m "Commit message"

Alternatively, if we wish to preserve the changes without committing them yet, we can use the git stash command to temporarily save our work:

git stash

Later, we can reapply them after returning to the original branch and using the git stash pop command.

What if the remote branch has the same name as a local branch?

Branch names must be unique with Git. If we get an error saying that the branch already exists, it means that there’s a local branch that has the same name. This could mean two things:

  1. We already checked out this branch before. In this case, we can directly switch to it without fetching using the command git checkout <branch_name>. Note that we dropped the -b parameter which is used to indicate we want to create a new branch. Since the branch already exists, we don’t need to recreate it.
  2. At some point, we create a different branch that happens to have the same. We can resolve this by using a different branch name with git checkout -b <different_name> origin/<branch_name>.

Can I make changes directly to a remote branch?

No, changes on a Git branch are made by first creating a local copy of a branch as we learned above, and then creating commits representing the changes we want.

How do I update my local branch with the latest remote changes?

To update our local branch with the latest changes from its corresponding remote branch, we typically use the git pull command:

git pull origin <branch_name>

This performs both a git fetch and a git merge to update our local branch. We learned that git fetch updated the local repository with the latest remote version. The git merge command merges those changes into our local branch. For more details, check this Git push and pull tutorial.

I get an error: "remote branch not found." How do I fix it?

This error indicates that the branch doesn’t exist in our local copy of the repository. So either:

  1. The branch doesn’t exist, which might indicate a typo in the branch name or that it’s an old branch that was already deleted.
  2. The local repository isn’t up to date. As we learned above, we can update it using git fetch.

Photo of François Aubry
Author
François Aubry
LinkedIn
Teaching has always been my passion. From my early days as a student, I eagerly sought out opportunities to tutor and assist other students. This passion led me to pursue a PhD, where I also served as a teaching assistant to support my academic endeavors. During those years, I found immense fulfillment in the traditional classroom setting, fostering connections and facilitating learning. However, with the advent of online learning platforms, I recognized the transformative potential of digital education. In fact, I was actively involved in the development of one such platform at our university. I am deeply committed to integrating traditional teaching principles with innovative digital methodologies. My passion is to create courses that are not only engaging and informative but also accessible to learners in this digital age.
Topics

Learn more about Git!

Course

Introduction to Git

4 hr
38.6K
Familiarize yourself with Git for version control. Explore how to track, compare, modify, and revert files, as well as collaborate with colleagues using Git.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

Git Clone Branch: A Step-by-Step Tutorial

To clone a specific branch, use the command git clone --single-branch --branch , replacing with the desired branch and with the repository's URL.
François Aubry's photo

François Aubry

tutorial

Git Rename Branch: How to Rename Local or Remote Branch

Learn how to rename local and remote Git branches using either the terminal or the graphical user interface (GUI) of popular clients like GitHub.
François Aubry's photo

François Aubry

5 min

tutorial

Git Switch Branch: A Guide With Practical Examples

Learn how to switch a branch in Git using git switch and understand the differences between git switch and git checkout.
François Aubry's photo

François Aubry

8 min

tutorial

Git Pull Force: How to Overwrite a Local Branch With Remote

Learn why git pull --force isn’t the best way to overwrite a local branch with the remote version, and discover the proper method using git fetch and git reset.
François Aubry's photo

François Aubry

tutorial

How to Clone a Specific Branch In Git

Learn how to clone only a single branch from a Git repository to save disk space and reduce cloning time.
Bex Tuychiev's photo

Bex Tuychiev

6 min

tutorial

GIT Push and Pull Tutorial

Learn how to perform Git PUSH and PULL requests through GitHub Desktop and the Command-Line.

Olivia Smith

13 min

See MoreSee More