git fetch origin doesn't fetch all branches

后端 未结 2 1982
-上瘾入骨i
-上瘾入骨i 2021-02-13 19:51

I read in the answers to this question that git fetch origin should fetch all branches of origin. In my case, it doesn\'t seem to give me any branches, tho

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 20:19

    Although this question was answered by the question asker, here is the real deal: the reason you are getting confused is because git fetch will show you the gory details of what it does on a new remote, but it will not show you anything (git clone) when cloning the repo at first. So cloning only creates the local master branch. Because it checks it out, by default. Checking it out creates it locally.

    So basically what you need to do is what you indicated in the beginning: check out all branches individually. So basically what you get is this:

    git branch -a | grep origin | sed "s@.*origin/@@" | while read f; do
      git checkout $f
    done
    

    There are probably more low-level "plumbing" commands that can do it, but basically this is it from a user perspective. Actually, the answer seems to be that you can create local branches directly.

    git branch -a | grep origin | sed "s@.*origin/@@" | while read f; do
      git branch $f origin/$f
    done
    

    But this won't set up tracking branches, probably. Answer from How to clone all remote branches in Git?

提交回复
热议问题