How do I check out a remote Git branch?

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

    I tried the above solution, but it didn't work. Try this, it works:

    git fetch origin 'remote_branch':'local_branch_name'
    

    This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name and track the remote one in it.

    0 讨论(0)
  • 2020-11-22 00:20

    You can start tracking all remote branches with the following Bash script:

    #!/bin/bash
    git fetch --all
    for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`
      do git branch -f --track "$branch" "origin/$branch"
    done
    

    Here is also a single-line version:

    git fetch --all; for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`; do git branch --track "$branch" "origin/$branch" ; done ;
    
    0 讨论(0)
  • 2020-11-22 00:25

    Commands

    git fetch --all
    git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>
    

    are equal to

     git fetch --all
    

    and then

     git checkout -b fixes_for_dev origin/development
    

    Both will create a latest fixes_for_dev from development

    0 讨论(0)
  • 2020-11-22 00:27

    First, you need to do:

    git fetch # If you don't know about branch name

    git fetch origin branch_name
    

    Second, you can check out remote branch into your local by:

    git checkout -b branch_name origin/branch_name
    

    -b will create new branch in specified name from your selected remote branch.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-11-22 00:29

    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.

    0 讨论(0)
提交回复
热议问题