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?


    Update

    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 <name of remote>/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
    

    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
    

    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.

    链接地址: http://www.djcxy.com/p/56.html

    上一篇: “在AngularJS中思考”如果我有一个jQuery背景?

    下一篇: 如何检出远程Git分支?