How to Check Out a Remote Branch in Git

How to Checkout a Remote Branch in Git

One common task in Git is working with remote branches. A remote branch is a branch that’s stored in a remote repository. Unlike local branches, which exist only on your machine, remote branches are shared with others.

You might encounter situations where you need to review someone else’s work or continue work on a feature that started remotely. That’s where checking out a remote branch comes in handy.

In this guide, I’ll walk you through the steps to check out a remote branch in Git.

Let’s dive right in and improve your Git workflow.

How to List Remote Branches

First, let’s see how to list all remote branches. This helps you know what’s available before checking out any branch.

Use this command to list remote branches:

 # git branch -r

Output.

origin/feature-branch
origin/main
origin/development

This command shows all the branches on the remote repository. In this example, origin is the name of the remote, and the branches are feature-branch, main, and development.

Checking Out a Remote Branch

Now, let’s get to the main task: checking out a remote branch.

To check out a remote branch, you typically use the git checkout command followed by the branch name. Here’s how:

 # git checkout feature-branch

If feature-branch exists remotely but not locally, Git automatically sets it up for tracking and switches to it.

Branch 'feature-branch' set up to track remote branch 'feature-branch' from 'origin'.
Switched to a new branch 'feature-branch'

Now, you are working on the feature-branch locally. Any changes you make can later be pushed back to the remote branch.

Tracking a Remote Branch

When you check out a remote branch, Git usually tracks it automatically. This means your local branch will be linked to the remote branch.

If you want to make sure your branch tracks the remote branch, you can use this command:

 # git checkout -b feature-branch origin/feature-branch

Output.

Switched to a new branch 'feature-branch'
Branch 'feature-branch' set up to track remote branch 'feature-branch' from 'origin'.

Here, git checkout -b creates a new branch called feature-branch and sets it to track the remote branch with the same name. Tracking makes it easier to pull updates from the remote branch and push your changes.

Conclusion

Checking out a remote branch is a fundamental skill in Git. It lets you collaborate with others and keep your work in sync. By following the steps in this guide, you can easily check out and work on remote branches. Don’t forget to follow best practices to keep your branches clean and up-to-date. Happy coding!

FAQs

1. What is a remote branch in Git?

A remote branch in Git is a branch stored in a remote repository, shared with other developers working on the project.

2. How do I switch to a remote branch in Git?

You can switch to a remote branch using git checkout branch_name. If the branch doesn't exist locally, Git will create and track it.

3. How do I list all remote branches in Git?

Use the command git branch -r to list all remote branches available in the repository.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva