How do I check out a remote Git branch?

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

    Fetch from the remote and checkout the branch.

    git fetch <remote_name> && git checkout <branch_name> 
    

    E.g.:

    git fetch origin && git checkout feature/XYZ-1234-Add-alerts

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

    This will DWIM for a remote not named origin (documentation):

    $ git checkout -t remote_name/remote_branch
    

    To add a new remote, you will need to do the following first:

    $ git remote add remote_name location_of_remote
    $ git fetch remote_name
    

    The first tells Git the remote exists, the second gets the commits.

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

    To get newly created branches

    git fetch
    

    To switch into another branch

    git checkout BranchName
    
    0 讨论(0)
  • 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 <name of remote>/test
    

    or the shorthand

    git checkout -t <name of remote>/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
    
    0 讨论(0)
  • 2020-11-22 00:45

    To clone a Git repository, do:

    git clone <either ssh url /http url>
    

    The above command checks out all of the branches, but only the master branch will be initialized. If you want to checkout the other branches, do:

    git checkout -t origin/future_branch (for example)
    

    This command checks out the remote branch, and your local branch name will be same as the remote branch.

    If you want to override your local branch name on checkout:

    git checkout -t -b enhancement origin/future_branch
    

    Now your local branch name is enhancement, but your remote branch name is future_branch.

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

    For us, it seems the remote.origin.fetch configuration gave a problem. Therefore, we could not see any other remote branches than master, so git fetch [--all] did not help. Neither git checkout mybranch nor git checkout -b mybranch --track origin/mybranch did work, although it certainly was at remote.

    The previous configuration only allowed master to be fetched:

    $ git config --list | grep fetch
    remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master
    

    Fix it by using * and fetch the new information from origin:

    $ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
    
    $ git fetch
    ...
     * [new branch] ...
    ...
    

    Now we could git checkout the remote branch locally.

    No idea how this config ended up in our local repo.

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