Juri Glass Juri Glass - 1 year ago 120
Git Question

How to check out a remote Git branch?

I am trying to check out a remote branch:

Somebody pushed a branch called

git push origin test
to a shared repository. I can see the branch with
git branch -r
. But how can I get this branch?

  • git checkout test
    does nothing

  • git checkout origin/test
    does something, but
    git branch
    * (no branch)
    . I am on no branch?

How do I share branches via a public repository?

Answer Source


Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, you can just do:

git fetch
git checkout test

(User masukomi points out below that git checkout test will NOT work in modern git if you have multiple remotes. In this case use git checkout -b test remote-name/test)

Old Answer

Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.

To fetch a branch, you simply need to:

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, giving you a local working copy:

git checkout -b test origin/test
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download