Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Now I'm trying to check out the remote test branch.

I've tried:

  • git checkout test which does nothing

  • git checkout origin/test gives * (no branch). Which is confusing. How can I be on "no branch"?

How do I check out a remote Git branch?

like image 419
Juri Glass Avatar asked Nov 23 '09 14:11

Juri Glass


People also ask

Can you checkout a remote branch in git?

Git Checkout a Remote BranchIn order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .


2 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.

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 
like image 146
hallski Avatar answered Sep 24 '22 10:09

hallski


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 

A more modern approach as suggested in the comments:

@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

like image 25
Jakub Narębski Avatar answered Sep 25 '22 10:09

Jakub Narębski