How do I check out a remote Git branch?

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

    Other guys and gals give the solutions, but maybe I can tell you why.

    git checkout test which does nothing

    Does nothing doesn't equal doesn't work, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?

    If the answer is 'yes', I can tell you the cause.

    The cause is that there is a file (or folder) named 'test' in your work tree.

    When git checkout xxx parsed,

    1. Git looks on xxx as a branch name at first, but there isn't any branch named test.
    2. Then Git thinks xxx is a path, and fortunately (or unfortunately), there is a file named test. So git checkout xxx means discard any modification in xxx file.
    3. If there isn't file named xxx either, then Git will try to create the xxx according to some rules. One of the rules is create a branch named xxx if remotes/origin/xxx exists.

提交回复
热议问题