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. With Git 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 as GitHub, GitLab, or Bitbucket.
In this guide, we’ll discuss how to checkout remote branches in Git using the git checkout command. We’ll learn how the command works, explore some examples, and understand the differences between git checkout and git switch to 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 to clone a remote repository which we’ll work on. For this tutorial, we will be cloning the Codecademy Docs remote 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 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. 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) named docs will appear in the workspace.
Next, navigate to the cloned repository:
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 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 we’re already inside the cloned remote repository, we can list all the remote branches in the repository using the git branch -r command:
git branch -rorigin/HEAD -> origin/mainorigin/main
The output, i.e., the lines that follow the git branch -r command, indicates the remote branches that exist in the repository.
Next, let’s understand how to fetch existing remote branches in a Git repository using git fetch.
How to fetch remote branches in Git using git fetch
To check out the state of a remote branch in the local repository, fetching remote branches is crucial. The git fetch command is a useful tool for performing this operation. This command fetches a remote branch to the local repository.
Before checking out an example for the git fetch command, let’s look at the syntax for it:
git fetch repo-name branch-name
In the syntax, 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 cppentryFrom 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 -rorigin/HEAD -> origin/mainorigin/agraves.test-branchorigin/cppentryorigin/dotnetorigin/full...
Besides git fetch, we can also use the git pull command to fetch remote branches in Git. Let’s learn how it works.
How to fetch remote branches in Git using git pull
The next command on the list is git pull. The git pull command 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, unlike git fetch.
Here’s the syntax for git pull:
git pull repo-name branch-name
In the syntax, repo-name indicates the remote repository from which the remote branch branch-name is to be fetched.
This example uses the git pull command to fetch the remote branch dotnet from origin:
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, 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 that we know how to list remote branches in Git, let’s see how to check out a remote branch using git checkout.
How to checkout remote branches in Git using git checkout
Checking out remote branches allows us to work on specific features or fixes that are available on a remote repository. The git checkout command is an efficient way to check out a remote branch in Git.
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 fullFrom 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.
Another command that we can use to check out a remote branch in Git is git switch. Let’s see how it works.
How to checkout remote branches in Git using git switch
The next command on the list is git switch, which is a newer alternative to git checkout. This command also helps us switch to a remote branch in Git efficiently.
Here is an example that 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 sqltermsgit switch -c sqlterms origin/sqlterms
With both git checkout and git switch covered, let’s see how they differ in their functionalities.
Differences between git checkout and git switch
Here are the differences between git checkout and git switch:
| Feature | git checkout |
git switch |
|---|---|---|
| Purpose | Used for switching branches and other tasks like restoring files | Specifically designed to switch branches |
| Introduced in | Available since early versions of Git | Introduced in Git 2.23 |
| Command scope | Multi-purpose: switch branches, restore files, create branches, etc. | Single-purpose: limited to switching or creating branches |
| Syntax for switching branches | git checkout branch-name |
git switch branch-name |
| Syntax for creating branches | git checkout -b new-branch |
git switch -c new-branch |
| Risk of mistakes | Higher (accidental file changes or overwrites) | Lower (clear and focused on switching only) |
| User-friendliness | Less intuitive for beginners | More intuitive and beginner-friendly |
Conclusion
In this tutorial, we covered how to checkout remote branches in Git using git checkout and git switch and 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.
Mastering git checkout is 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 the Learn Git: Branching and Collaboration course on Codecademy.
Frequently asked questions
1. Why am I getting an error when trying to checkout a remote branch using git 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
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.'
Meet the full teamRelated articles
- Article
How to Switch Branches in Git Using Git Switch (With Examples)
Learn how to switch branches in Git using the `git switch` command. Explore local and remote branches, common errors, and tips for smoother branch management. - Article
How to Delete a Git Branch Locally and Remotely
Learn how to delete a Git branch both locally and remotely. This tutorial covers git delete branch commands and common errors, which will help you keep your repositories clean and organized. - Article
How to Use Git: A Step-by-Step Tutorial
Learn how to use Git efficiently to track changes in your code, collaborate with others, and simplify your development process. Master version control with our step-by-step guide!
Learn more on Codecademy
- Learn how to create, merge, clone, and fetch Git branches to collaborate with other developers.
- Beginner Friendly.1 hour
- Become proficient in Git for DevOps, including repository management, Git commands, GUI, distributed workflows, branching, Git server protocols, and Gitflow.
- Intermediate.1 hour
- Learn how to track changes in your code and switch between different versions with Git, an open-source version control system.
- Beginner Friendly.1 hour
- What are remote branches in Git?
- How to clone a remote repository in Git
- How to list remote branches in Git
- How to fetch remote branches in Git using `git fetch`
- How to fetch remote branches in Git using `git pull`
- How to checkout remote branches in Git using `git checkout`
- How to checkout remote branches in Git using `git switch`
- Differences between `git checkout` and `git switch`
- Conclusion
- Frequently asked questions