How Do I Check out a Remote Git Branch?
Somebody Pushed a Branch Called Test with Git Push Origin Test to a Shared Repository. I Can See the Branch with Git Branch -R. How Do I Check out the Remote...
Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r.
How do I check out the remote test branch? I've tried:
git checkout test, which does nothinggit checkout origin/testgives* (no branch)
40 Answers
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.
Must Read
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.