How can I check out a remote Git branch?
Categories:
How to Check Out a Remote Git Branch

Learn the essential Git commands to discover, fetch, and check out remote branches, enabling seamless collaboration and access to team contributions.
Working with Git often involves collaborating with others, which means interacting with remote repositories and their branches. While checking out local branches is straightforward, accessing branches that exist only on a remote server requires a few extra steps. This guide will walk you through the process of identifying, fetching, and checking out remote Git branches, ensuring you can easily access and work on your team's latest contributions.
Understanding Remote Branches
Before you can check out a remote branch, it's crucial to understand what it is. A remote branch is a reference to the state of branches in a remote repository. When you clone a repository, Git automatically sets up a remote named origin
(by default) that points to the server you cloned from. Remote-tracking branches (e.g., origin/main
, origin/feature-x
) are local copies of these remote branches. They are read-only and serve as bookmarks to the last known state of the remote repository.
graph TD A[Remote Repository] --> B{git clone} B --> C[Local Repository] C --> D[Local Branch (e.g., main)] A --> E[Remote Branch (e.g., origin/main)] E --> F[Remote-tracking Branch (e.g., origin/main)] F -- "Updates from" --> A
Relationship between remote and local branches in Git
Step 1: Discovering Remote Branches
The first step is to find out which branches are available on the remote repository. You can do this using the git branch
command with the -r
(remote) or -a
(all) flags. The -a
flag shows both local and remote-tracking branches.
git branch -r
List all remote-tracking branches.
git branch -a
List all local and remote-tracking branches.
git branch -r
will show branches prefixed with the remote name, like origin/feature/new-feature
. This indicates that feature/new-feature
exists on the origin
remote.Step 2: Fetching the Latest Remote Information
Sometimes, your local repository might not have the most up-to-date information about remote branches. New branches might have been pushed to the remote since your last git fetch
or git pull
. To ensure you have the latest references, you should fetch from the remote.
git fetch origin
Fetch all new branches and commits from the 'origin' remote.
git fetch
downloads new data from a remote repository but doesn't integrate it into your working files. It updates your remote-tracking branches.Step 3: Checking Out a Remote Branch
Once you've identified the remote branch and ensured your local repository is up-to-date, you can check it out. Git provides a convenient shortcut for this. If you try to check out a branch name that exists on only one remote (e.g., origin/feature-x
), Git will automatically create a new local branch with the same name and set it up to track the remote branch.
git checkout <branch-name>
Checkout a remote branch, creating a local tracking branch.
For example, if you saw origin/feature/new-feature
in your git branch -r
output, you would run:
git checkout feature/new-feature
Example of checking out a remote branch named 'feature/new-feature'.
feature/new-feature
that tracks origin/feature/new-feature
. This is the most common and recommended way to check out a remote branch.Alternative: Explicitly Creating a Tracking Branch
While the automatic checkout is convenient, you can also explicitly create a local branch that tracks a remote branch, especially if you want your local branch to have a different name.
git checkout -b <local-branch-name> origin/<remote-branch-name>
Create a new local branch and set it to track a specific remote branch.
For instance, to create a local branch named my-new-feature
that tracks origin/feature/new-feature
:
git checkout -b my-new-feature origin/feature/new-feature
Explicitly creating 'my-new-feature' tracking 'origin/feature/new-feature'.
git checkout origin/feature/new-feature
directly, you will be in a 'detached HEAD' state. This means you are directly on a remote-tracking branch, not a local branch. While you can look around, any commits you make will not belong to a branch and can be lost easily. Always create a local branch to work on.