PRO 30% Free • Stripe ($15) & Razorpay (₹899)

How to Checkout Remote Branches in Git Using Git Checkout

Top Course Staff · 6 min read · Developer tools · PRO · 30% Free Preview Available

How to Checkout Remote Branches in Git Using Git Checkout

What are remote branches in Git?

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

There are two types of Git branches - 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 asGitHub, GitLab, or Bitbucket.

In this guide, we’ll discuss how to checkout remote branches in Git using thegit checkoutcommand. We’ll learn how the command works, explore some examples, and understand the differences betweengit checkoutandgit switchto have a clear idea of when to use each.

Let’s start by discussing how to clone a remote repository in Git.

How to clone a remote repository in Git

Before we start interacting with remote branches, we need toclonea remote repository which we’ll work on. For this tutorial, we will be cloning theCodecademy Docsremote repository, i.e., making a local copy of the repository on our machine.

Let’s start the process by cloning the Codecademy Docs remote repository to the local machine by writing this command in theterminal:

git clone https://github.com/Codecademy/docs.git
Copy to clipboard
git clone https://github.com/Codecademy/docs.git

Thegit clonecommand enables us to clone a remote repository to the local machine. The URL used in this case points to the Codecademy Docs remote repository, which is cloned to our local workspace.

After cloning, a new folder (repository) nameddocswill appear in the workspace.

Next, navigate to the cloned repository:

cd docs
Copy to clipboard
cd docs

Since we’re now all set with a cloned remote repository in our local workspace, let’s discover how we can list remote branches in a Git repository.

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 thegit branch -rcommand.

If we dive deeper, thegit branchcommand allows us to get a list of all the local branches in a repository. However, attaching the-rflag, short for--remotes, enables us to obtain a list of all the remote branches in the repository.

Since we’re already inside the cloned remote repository, we can list all the remote branches in the repository using thegit branch -rcommand:

git branch -rorigin/HEAD -> origin/mainorigin/main
Copy to clipboard
git branch -rorigin/HEAD -> origin/mainorigin/main

The output, i.e., the lines that follow thegit branch -rcommand, indicates the remote branches that exist in the repository.

Next, let’s understand how to fetch existing remote branches in a Git repository usinggit fetch.

How to fetch remote branches in Git usinggit fetch

To check out the state of a remote branch in the local repository, fetching remote branches is crucial. Thegit fetchcommand is a useful tool for performing this operation. This command fetches a remote branch to the local repository.

Before checking out an example for thegit fetchcommand, let’s look at the syntax for it:

git fetch repo-name branch-name

In the syntax,repo-nameindicates the remote repository from which the remote branchbranch-nameis to be fetched.

Here’s an example that uses thegit fetchcommand to fetch the remote branchcppentryfromorigin, 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 cppentryFrom https://github.com/Codecademy/docs* branch              cppentry   -> FETCH_HEAD
Copy to clipboard
git fetch origin cppentryFrom https://github.com/Codecademy/docs* branch              cppentry   -> FETCH_HEAD

Moreover, we can use thegit fetchcommand to fetch all the remote branches fromoriginin one go:

git fetch origin
Copy to clipboard
git fetch origin

Let’s check out the list of fetched remote branches using thegit branch -rcommand:

git branch -rorigin/HEAD -> origin/mainorigin/agraves.test-branchorigin/cppentryorigin/dotnetorigin/full...
Copy to clipboard
git branch -rorigin/HEAD -> origin/mainorigin/agraves.test-branchorigin/cppentryorigin/dotnetorigin/full...

Besidesgit fetch, we can also use thegit pullcommand to fetch remote branches in Git. Let’s learn how it works.

How to fetch remote branches in Git usinggit pull

The next command on the list isgit pull. Thegit pullcommand also allows us to fetch existing remote branches in a Git repository. The only difference is that it merges the fetched changes with the current branch, unlikegit fetch.

Here’s the syntax forgit pull:

git pull repo-name branch-name

This example uses thegit pullcommand to fetch the remote branchdotnetfromorigin:

git pull origin dotnetFrom https://github.com/Codecademy/docs* branch              dotnet   -> FETCH_HEADAuto-merging README.mdMerge made by the 'ort' strategy.README.md | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Copy to clipboard
git pull origin dotnetFrom https://github.com/Codecademy/docs* branch              dotnet   -> FETCH_HEADAuto-merging README.mdMerge made by the 'ort' strategy.README.md | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)

As we can see above, thegit pullcommand merges the fetched remote branch with the working branch, whereas thegit fetchcommand 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 usegit fetch. On the other hand, if we want to merge the latest changes from a remote branch to the working branch, we can usegit pull.

Now that we know how to list remote branches in Git, let’s see how to check out a remote branch usinggit checkout.

How to checkout remote branches in Git usinggit checkout

Checking out remote branches allows us to work on specific features or fixes that are available on a remote repository. Thegit checkoutcommand is an efficient way to check out a remote branch in Git.

Let’s look at an example for thegit checkoutcommand. To start, fetch the remote branchfullfromoriginusing thegit fetchcommand:

git fetch origin fullFrom https://github.com/Codecademy/docs* branch              full   -> FETCH_HEAD
Copy to clipboard
git fetch origin fullFrom https://github.com/Codecademy/docs* branch              full   -> FETCH_HEAD

Then, switch to the fetched remote branchfullusing thegit checkoutcommand:

git checkout full
Copy to clipboard
git checkout full

Besidesgit checkout, we can also use thegit checkout -bcommand, where we can create and checkout a branch at once:

git checkout -b full origin/full
Copy to clipboard
git checkout -b full origin/full

Here,fullis the name of the local branch that we’re creating, andorigin/fullis the name of the remote branch. The name of the local branch can be the same or different from the remote branch.

Another command that we can use to check out a remote branch in Git isgit switch. Let’s see how it works.

How to checkout remote branches in Git usinggit switch

The next command on the list isgit switch, which is a newer alternative togit checkout. This command also helps us switch to a remote branch in Git efficiently.

Here is an example that uses thegit switchcommand to fetch the remote branchsqltermsfromoriginand switch to it:

git switch sqlterms
Copy to clipboard
git switch sqlterms

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

git fetch origin sqltermsgit switch -c sqlterms origin/sqlterms
Copy to clipboard
git fetch origin sqltermsgit switch -c sqlterms origin/sqlterms

With bothgit checkoutandgit switchcovered, let’s see how they differ in their functionalities.

Differences betweengit checkoutandgit switch

Here are the differences betweengit checkoutandgit switch:

Conclusion

In this tutorial, we covered how to checkout remote branches in Git usinggit checkoutandgit switchand understood the differences between them. We also learned how to clone, list, and fetch remote branches, making us ready to work with remote branches efficiently.

Masteringgit checkoutis essential for collaborating on projects effectively. By understanding how to checkout a remote branch and interact with it, we can streamline the development process and contribute to projects seamlessly.

If you want to learn more about how to interact with Git branches, check out theLearn Git: Branching and Collaborationcourse on Codecademy.

Frequently asked questions

1. Why am I getting an error when trying to checkout a remote branch usinggit checkout?

Common reasons include:

  • The remote branch doesn’t exist.
  • You haven’t fetched the latest changes.
  • You have typed the command incorrectly.

2. How do I see which remote branch my local branch is tracking?

Use:

git branch -vv
Copy to clipboard
git branch -vv

This shows each local branch, the remote branch it’s tracking, and its commit status.

3. Is it necessary to create a local branch to work with a remote branch?

Yes. Git requires a local branch for you to make changes and commit. Even when you checkout a remote branch, Git sets up a local branch behind the scenes.

'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.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Production AI Blueprints

Ready to build production-grade AI systems?

Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.