Easily Perform Git Checkout Remote Branch [Step-by-Step]

Published:20 October 2023 - 8 min. read

Devin Rich Image

Devin Rich

Read more tutorials by Devin Rich!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Are you a newcomer to a team and been instructed to checkout a remote branch from the Git code repository? Whether part of a sprawling enterprise or a compact team, mastering remote Git branches is key to seamless code collaboration. In this guide, delve into the process of retrieving code from a remote Git repository, focusing on the ‘git checkout remote branch’ command.

Not a reader? Explore this related video tutorial!
Not seeing the video? Ensure your ad blocker is disabled.

Prerequisites

Other potential installation methods for Windows include Chocolatey and Git4Win.

Understanding a Remote Branch

Git repositories chronicle and track a sequence of related commits within a branch. By default, a commit resides in the currently active branch, usually termed main or master.

A remote branch is a branch located in a remote Git repository. These remote repositories, termed as “remotes,” are the destinations your local Git repository will scout for external commit updates once set up.

Discover available branches using the Git branch command. The branch marked with an asterisk (*) is the active one. The branches listed below are solely local as no remote branches have been fetched yet.

git branch
git checkout remote branch - List of local branches shown from the git branch command.
List of local branches shown from the git branch command.

Mastering Git Checkout Remote Branch

Suppose you have a local Git repository and the need arises to collaborate with other developers. This is where the Git checkout command comes into play, enabling you to fetch the latest commits from a remote branch on a remote Git repository.

Once a remote branch is configured, Git will mirror the remote branch instead of a local-only branch. Below is an example of the command git checkout remote <branch>.

git checkout remote main

Clone the Remote Git Repository

Now that an existing repository is at your disposal, it’s time to dive in. Follow the steps below to clone the remote repository to a local folder on your machine.

1. It’s often simpler to clone a remote repository via the HTTPS URL rather than SSH to sidestep certificate and firewall issues.

  • Head over to the repository on GitHub.
  • Hit the Code button and select HTTPS.
  • Click the Clipboard icon adjacent to the link to copy the URL to your clipboard.
git checkout remote branch - Locating the HTTPS clone URL in GitHub.
Locating the HTTPS clone URL in GitHub.

2. Having copied the HTTPS URL to your clipboard, open a command-line interface using your preferred CLI, such as Windows Command Prompt or the Windows Terminal.

3. Navigate to the folder where you wish the cloned repository to reside. For this tutorial, the git folder in the home directory is utilized as depicted in the screenshots. Execute the following command to clone the repository, which creates a sub-folder housing the remote repository.

# GitHub git clone https://github.com/Adam-the-Automator/git-checkout-remote-branch
git checkout remote branch - Cloning a remote repository to the local machine.
Cloning a remote repository to the local machine.

4. Traverse to the cloned repository with the command, cd git-checkout-remote-branch.

Execute the following status command to ascertain that the repository has been successfully cloned. The Git status command unveils the differences between the local branch and remote branch, aiding in determining if your branch is connected and updated! The default Git remote is termed origin, prefixed to the main remote branch. git status

git status
git checkout remote branch - Checking a repositories status.
Checking a repositories status.

For most modern Git software, main is the default primary branch. You may encounter an older application that uses master for their default branch name, but both names are functionally the same. This tutorial uses branches named main, RemoteBranch, SomeBranch, and AnotherBranch.

What is a Git Remote and How to Track Remote Branches

Cloning a repository automatically creates a reference to a remote source, known as a remote. This reference is used to check for new commits or push new changes to the remote repository.

A Git repository itself may have multiple remotes, but a branch can only reference a single remote.

Tracking a remote branch establishes a relationship with a local branch. This relationship enables you to effortlessly push or pull commits from the remote branch to the local branch. Moreover, a tracked branch determines how far ahead or behind in commits the local branch is from the remote.

Listing a Git Repository’s Remotes

Firstly, ensure the repository you’re working with has any Git remotes. To do this, view Git remotes in your repository with the git remote command as shown below. In this instance, two remotes are displayed, origin and remote2.

git remote
git checkout remote branch - Viewing a repositories two configured remotes.
Viewing a repositories two configured remotes.

Two additional tracked remote repositories are named origin and remote2, where your remotes may be different.

To glean more detail about the configured remotes, add the v switch to display the full Git remote URI.

git remote -v
git checkout remote branch - Viewing verbose details about repo remotes.
Viewing verbose details about repo remotes.

You may wonder why the separate URI’s for fetch and push. Some Git workflows allow you to check for updates to a repository at a different URI than where you may send changes, this is called the triangular workflow.

Viewing Available Remote Branches

Display all available branches, both local and remote, in your repository with the branch command. To show the remote branches as well, append the all switch, as exemplified below.

git branch --all

In the image below, the remote repository origin used in this tutorial is shown as anticipated. Nonetheless, this image omits all branches from the remote named Remote2.

git checkout remote branch - Viewing all known branches in a repo (including remotes).
Viewing all known branches in a repo (including remotes).

If a branch on your remote isn’t showing on the branch list, you must update your list of available remote branches as covered in the next section.

Updating the Available Remote Branches

At times, a remote repository may add or alter branches. To ensure you are working with the most up-to-date branches, it’s necessary to run the git fetch command.

The fetch command connects to the remote repository, retrieving a list of all available branches. View your currently tracked branches post running the git fetch command to see that the remote branch is now visible, as demonstrated below.

# Retrieve available remote branches
git fetch

# Verify that all branches are now available on the local Git repository
git branch --all

Below, the branch remotes/origin/RemoteBranch is now visible in the list of available remote branches post executing the git fetch command.

git checkout remote branch - Using git fetch to update the list of available remote branches.
Using git fetch to update the list of available remote branches.

The fetch command only checks for updates from the remote that’s currently tracked in the present branch. To have Git check for updates from all remotes in the repo, irrespective of tracking status, append the all parameter.

git fetch --all

To fetch the available branches from a specific remote, include the remote name in the git fetch command, like Remote2 in the example below, which retrieves the branch, main.

# List available branches in a local Git repository
git branch --all
# Fetch available branches from remote2
git fetch Remote2
# Verify that the remote2 branch, main, is now available
git branch --all
Fetching main from remote2 with the git fetch command.
Fetching main from remote2 with the git fetch command.

Any time you check for remote updates with git fetch, git pull, or git push, Git compares commits with the remote branch. Local branches can track any Git remote branch. Additionally, multiple local branches can set to track the same remote branch.

Viewing Each Branch’s Remote Tracking

To see the tracked remote branch alongside the corresponding local branch, employ the vv parameter with the command git branch. The command results display a list of remotes and branches in the format [<remote>/<branch>] as illustrated below.

git branch -vv
Listing remote tracking of all local branches.
Listing remote tracking of all local branches.

Altering a Branch’s Remote Tracking

There might be a necessity to alter your local branch to track a different remote branch. To set your current branch to a specific remote branch, employ the u parameter, as illustrated below.

git branch -u Remote2/main
Setting what remote to track for the current branch.
Setting what remote to track for the current branch.

Checking out Git Remote Branches

With new branches created on the remote Git repository, you may need to perform a git checkout remote branch operation. Checking out a remote repository branch is executed through the checkout command.

If the following conditions are satisfied, Git will autonomously determine the right remote, download a local copy of the remote branch, and set the local copy to track against the remote branch. Verify the following conditions are not met through the results of git branch --all command.

  • A local branch with the same name does not already exist.
  • Multiple remotes that use the same remote branch name do not exist.

To perform the git checkout remote branch operation, use the git checkout command and type the name of the remote branch as seen below.

git checkout RemoteBranch
git checkout remote branch - Creating a local branch from a remote branch with git checkout.
Creating a local branch from a remote branch with git checkout.

If your local repository already contains a local branch with the same name, add the b parameter to specify an alternate local branch name before the remote branch to checkout.

git checkout -b AnotherBranch origin/RemoteBranch
Creating a local copy of a remote branch with an alternate local branch name.
Creating a local copy of a remote branch with an alternate local branch name.

If your local repository has multiple remotes with the same branch name, specify which remote and branch to track local commits against.

git checkout --track origin/RemoteBranch
git checkout remote branch with tracking enabled - Creating a local copy of a remote branch.
Creating a local copy of a remote branch with tracking enabled.

Future versions of Git may possibly prefer that you use the git switch command for changing and creating branches. The Git switch command is only for branches while the Git checkout command has many abilities. Irshad S. wrote a nice primer on the rationale if you’d like to read more about it.

Pushing a Local Branch to a Remote

Upon creating a local branch devoid of a corresponding remote branch for tracking, you can propel the branch to the remote using the git push command. The most straightforward method to establish a new branch on your preferred remote is by employing the u parameter.

The u parameter, as utilized earlier, necessitates the arguments of the repository name and the source branch. By solely specifying HEAD as the source branch name, Git comprehends to use the current local branch name as the name on the remote, in this case, NewBranch.

# Checkout and switch to NewBranch
git checkout -b NewBranch
# Create NewBranch on the origin remote
git push -u origin HEAD
# Verify that the branches on the remote now exist
git branch -vv
Pushing a local branch to an upstream remote named origin with the same name as local.
Pushing a local branch to an upstream remote named origin with the same name as local.

HEAD is a shortcut that simply means the current branch. You can push other branches to the remote and establish tracking by using their name instead of HEAD.

At times, the name of the current local branch does not match with the remote branch’s name. To utilize a distinct name, specify the desired branch name after the source, joined by a colon, as demonstrated below.

# Create the current branch on the origin remote as AnotherBranch
git push -u origin HEAD:AnotherBranch
# Verify that the local NewBranch now exists as AnotherBranch on the origin remote
git branch -vv
Pushing a local branch to an upstream remote named origin with an alternate remote branch name.
Pushing a local branch to an upstream remote named origin with an alternate remote branch name.

Next Steps and Additional Git Resources

Having reviewed the essentials of Git, managing remotes, and performing git checkout remote branch, you are now equipped with a wealth of knowledge that will aid in averting future frustrations. The steps to checkout remote branch in Git and best practices shared will serve as invaluable resources moving forward.

Should you wish to delve deeper into other facets of managing a Git repository, the following resources are highly recommended:

  • A primer on detached heads: Discover scenarios where a detached HEAD is beneficial or inadvertent, and how to handle it.
  • Git switch: A command dedicated to dealing with branches, unlike the multifaceted Git checkout.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!