git checkout - How do I check out a remote Git branch?

ID : 19

viewed : 194

Tags : gitgit-checkoutremote-branchgit

Top 5 Answer for git checkout - How do I check out a remote Git branch?

vote vote


The answer has been split depending on whether there is one remote repository configured or multiple. The reason for this is that for the single remote case, some of the commands can be simplified as there is less ambiguity.

Updated for Git 2.23: For older versions, see the section at the end.

With One Remote

In both cases, start by fetching from the remote repository to make sure you have all the latest changes downloaded.

$ git fetch 

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a  ... remotes/origin/test 

The branches that start with remotes/* can be thought of as read only copies of the remote branches. To work on a branch you need to create a local branch from it. This is done with the Git command switch (since Git 2.23) by giving it the name of the remote branch (minus the remote name):

$ git switch test 

In this case Git is guessing (can be disabled with --no-guess) that you are trying to checkout and track the remote branch with the same name.

With Multiple Remotes

In the case where multiple remote repositories exist, the remote repository needs to be explicitly named.

As before, start by fetching the latest remote changes:

$ git fetch origin 

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a 

With the remote branches in hand, you now need to check out the branch you are interested in with -c to create a new local branch:

$ git switch -c test origin/test 

For more information about using git switch:

$ man git-switch 

I also created the image below for you to share the differences, look at how to fetch works, and also how it's different to pull:

enter image description here

Prior to Git 2.23

git switch was added in Git 2.23, prior to this git checkout was used to switch branches.

To checkout out with only a single remote repository:

git checkout test 

if there there are multiple remote repositories configured it becomes a bit longer

git checkout -b test <name of remote>/test 
vote vote


Sidenote: With modern Git (>= 1.6.6), you are able to use just

git checkout test 

(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.

The * (no branch) in git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:

git checkout -b test HEAD 

** EDIT (by editor not author) **

I found a comment buried below which seems to modernize this answer:

@Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) – Jakub Narębski Jan 9 '14 at 8:17

emphasis on git checkout origin/test

vote vote


In this case, you probably want to create a local test branch which is tracking the remote test branch:

$ git branch test origin/test 

In earlier versions of git, you needed an explicit --track option, but that is the default now when you are branching off a remote branch.

vote vote


Accepted answer not working for you?

While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

$ git checkout -b remote_branch origin/remote_branch 

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?


If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

$ git fetch origin remote: Counting objects: 140, done. remote: Compressing objects: 100% (30/30), done. remote: Total 69 (delta 36), reused 66 (delta 33) Unpacking objects: 100% (69/69), done. From    e6ef1e0..5029161  develop    -> origin/develop  * [new branch]      demo       -> origin/demo    d80f8d7..359eab0  master     -> origin/master  $ git checkout demo Branch demo set up to track remote branch demo from origin. Switched to a new branch 'demo' 

As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.

vote vote


I tried the above solution, but it didn't work. Try this, it works:

git fetch origin 'remote_branch':'local_branch_name' 

This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name and track the remote one in it.

Top 3 video Explaining git checkout - How do I check out a remote Git branch?