How do I check out a remote Git branch?

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

    OK, the answer is easy... You basically see the branch, but you don't have a local copy yet!...

    You need to fetch the branch...

    You can simply fetch and then checkout to the branch, use the one line command below to do that:

    git fetch && git checkout test
    

    I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:

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

    If the remote branch name begins with special characteres you need to use single quotes around it in the checkout command, or else git won't know which branch you are talking about.

    For example, I tried to checkout a remote branch named as #9773 but the command didn't work properly, as shown in the picture below:

    For some reason I wondered if the sharp symbol (#) could have something to do with it, and then I tried surrounding the branch name with single quotes, like '#9773' rathen than just #9773, and fortunately it worked fine.

    $ git checkout -b '#9773' origin/'#9773'
    
    0 讨论(0)
  • 2020-11-22 00:32

    Accepted answer not working for you?

    While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

    $ git checkout -b remote_branch origin/remote_branch
    

    fatal: git checkout: updating paths is incompatible with switching branches.
    Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

    Solution

    If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

    $ git fetch origin
    remote: Counting objects: 140, done.
    remote: Compressing objects: 100% (30/30), done.
    remote: Total 69 (delta 36), reused 66 (delta 33)
    Unpacking objects: 100% (69/69), done.
    From https://github.com/githubuser/repo-name
       e6ef1e0..5029161  develop    -> origin/develop
     * [new branch]      demo       -> origin/demo
       d80f8d7..359eab0  master     -> origin/master
    
    $ git checkout demo
    Branch demo set up to track remote branch demo from origin.
    Switched to a new branch 'demo'
    

    As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.

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

    I was stuck in a situation seeing error: pathspec 'desired-branch' did not match any file(s) known to git. for all of the suggestions above. I'm on git version 1.8.3.1.

    So this worked for me:

    git fetch origin desired-branch
    git checkout -b desired-branch FETCH_HEAD
    

    The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:

    $ git fetch origin desired-branch
    From github.com:MYTEAM/my-repo
        * branch            desired-branch -> FETCH_HEAD
    
    0 讨论(0)
  • 2020-11-22 00:33

    to get all remote branches use this :

    git fetch --all
    

    then checkout to the branch :

    git checkout test
    
    0 讨论(0)
  • 2020-11-22 00:33

    Use fetch to pull all your remote

       git fetch --all
    

    To list remote branches:

       git branch -r
    

    For list all your branches

       git branch -l
       >>outpots like-
         * develop
           test
           master
    

    To checkout/change a branch

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