How to clone all remote branches in Git?

后端 未结 30 2809
情话喂你
情话喂你 2020-11-22 01:08

I have a master and a development branch, both pushed to GitHub. I\'ve cloned, pulled, and fetched, but I re

30条回答
  •  我在风中等你
    2020-11-22 01:32

    Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs) from one or more other repositories along with the objects necessary to complete their histories. In other words it fetches the objects which are reachable by the objects that are already downloaded. See: What does git fetch really do?

    Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all/git fetch --all won't help in that case, but you can list them by:

    git ls-remote -h -t origin
    

    and fetch them manually by knowing the ref names.

    So to fetch them all, try:

    git fetch origin --depth=10000 $(git ls-remote -h -t origin)
    

    The --depth=10000 parameter may help if you've shallowed repository.

    Then check all your branches again:

    git branch -avv
    

    If above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):

    $ git remote -v show origin
    ...
      Remote branches:
        master      tracked
    

    by git remote set-branches like:

    git remote set-branches --add origin missing_branch
    

    so it may appear under remotes/origin after fetch:

    $ git remote -v show origin
    ...
      Remote branches:
        missing_branch new (next fetch will store in remotes/origin)
    $ git fetch
    From github.com:Foo/Bar
     * [new branch]      missing_branch -> origin/missing_branch
    

    Troubleshooting

    If you still cannot get anything other than the master branch, check the followings:

    • Double check your remotes (git remote -v), e.g.
      • Validate that git config branch.master.remote is origin.
      • Check if origin points to the right URL via: git remote show origin (see this post).

提交回复
热议问题