How do I check out a remote Git branch?

后端 未结 30 2042
灰色年华
灰色年华 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:43

    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
    

    ** EDIT (by editor not author) **

    I found a comment buried below which seems to modernize this answer:

    @Dennis: git checkout , 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

提交回复
热议问题