How do I check out a remote Git branch?

后端 未结 30 2708
灰色年华
灰色年华 2020-11-22 00:12

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r.

<
30条回答
  •  清歌不尽
    2020-11-22 00:45

    With One Remote

    Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can do:

    git fetch
    git checkout test
    

    As user masukomi points out in a comment, git checkout test will NOT work in modern git if you have multiple remotes. In this case use

    git checkout -b test /test
    

    or the shorthand

    git checkout -t /test
    

    With >1 Remotes

    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
    

提交回复
热议问题