gitrefspec

Git checkout remote branch of a remote repository


When git clone some repository then it retrieves all branches from it:

git clone https://github.com/android/platform_build.git build1
cd build1/ && git branch -a && cd ..

then, when I use build1 as a mirror and clone it, I do not see all its remote branches (I see only build1's local master branch)

git clone build1 build2
cd build2 && git branch -a && cd ..

How can I checkout remote of remote branches ?

I know that I can checkout all remote branches in build1 mirror with command

cd build1 && for remote in `git branch -r `; do git branch --track $remote; done

Track all remote git branches as local branches but what if I do not have access to build1 directory ?

Also, there is git ls-remote origin command which shows all remote refs, but is there any elegant way to checkout those remote of remote branches ?


Solution

  • After you clone build1 to build2, build1 is build2's origin, and build2 knows nothing about https://github.com/android/platform_build.git , which is where the "remote" branches live. One solution may be to add https://github.com/android/platform_build.git as a remote in the configuration of build2. To add a remote called github:

    git remote add github https://github.com/android/platform_build.git
    

    Then run a fetch to get the remote branches:

    git fetch github
    

    Now, when you run git branch -a you should see branches prefixed with remotes/github/ that correspond with those prefixed with remotes/origin in build1.

    On the other hand, build2 should have a remote branch for each local branch in build1, including those that are tracking remote branches from build1's perspective. For example, if there is a branch named foo on github, it will show up as remotes/origin/foo in build1. If you set up a local branch to track that remote branch in build1 also called foo, then build2 should have a remotes/origin/foo as well. Then, when foo is updated in build1 by fetching/merging changes from github's foo, those changes should show up in build2 after a fetch/pull.