Checking out Remote Branches with Git

How do I check using git checkout from remote branch?

1 Like

With over a decade of experience working with Git, I’ve come across various setups and configurations. The method to switch branches in Git depends on whether you’re working with a single remote repository or multiple. For Git version 2.23 or newer, you should start by fetching the updates from the remote repository:

git fetch

If you’re dealing with a single remote, the new git switch command is very straightforward:

git switch test

For multiple remote repositories, specify the remote name to avoid confusion:

git switch -c test origin/test

In older versions of Git, before 2.23, you’d use the git checkout command:

git checkout test

And for multiple remotes:

git checkout -b test <remote name>/test

This ensures you’re working with the most current version of the branch.

Adding to what Priyada mentioned, and having used Git in various professional environments, it’s crucial to ensure all remote objects and references are fetched before attempting to switch branches. Failure to do so can lead to errors, like:


fatal: git checkout: updating paths is incompatible with switching branches.

Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

To avoid such issues, always fetch from your remote first:


$ git fetch origin

This command retrieves all the latest data from the remote named origin. After fetching, you can confidently switch to your desired branch:


$ git checkout remote_branch

For example, if you fetched the branch named demo from origin, you can now track it locally with ease.

Building on the previous insights and from my experiences troubleshooting Git issues, I once faced an error stating ““pathspec ‘desired-branch’ did not match any file(s) known to git”” while using an older Git version (1.8.3.1). However, this method proved effective:

git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD

Fetching a specific branch moves the latest commit of this branch to FETCH_HEAD, which is a temporary marker in Git:

git fetch origin desired-branch

From github.com:MYTEAM/my-repo
    * branch            desired-branch -> FETCH_HEAD

Using FETCH_HEAD to create a new local branch ensures you’re working directly from the freshly fetched remote branch, bypassing any pathspec errors.