Checking out a remote branch can be surprisingly difficult due to Git’s complexity.

This tutorial will explain how to do that step by step.

Clone the Remote Repository if you have not already.

Example: This clones the WordPress Repository on GitHub.

git clone https://github.com/WordPress/WordPress

Retrieving the Original Remote Repository

To ensure that your local copy of the repository matches the remote one, use the fetch command to retrieve the origin.

Example:

git fetch origin

List the Remote Branches Available to Checkout

You now want to list out the remote branches available for you to checkout with the branch command, using the -r option to list out the remote branches.

Example:

git branch -r

Tip: By default, Git will send the output to a program named “Less” that can be used to edit text. To exit Less and return to the command shell, press the q keyboard.

You can also add the –no-pager option, immediately after ‘git’, before the other options in the Git command line.

This will pipe the output back to the console instead of using Less to process the page of data.

Example:

git --no-pager branch -r

Check Out the Remote Branch for the First Time

Here’s where things get tricky.

Most likely, you want to clone the remote branch and switch to it without being in the detached head state.

To do that, you will need to use the -b option and supply the branch’s path from the origin.

Example:

git checkout -b 4.0-branch origin/4.0-branch

The output will let you know that the command has been executed successfully.

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

You can verify that you are on the correct branch with the following command:

git branch --show-current

Which should output:

4.0-branch

Checking out a remote Branch That You’ve Previously Checked out Before

If you have already checked out the remote branch, the command to switch to it is much simpler.

Example:

git checkout 5.0-branch

Which Should Output:

Switched to branch '5.0-branch'
Your branch is up to date with 'origin/5.0-branch'.