How to fetch all git branches?

I cloned a git repository, which contains about 5 branches. However, when I do git branch I only see one of them:

$ git branch
* master

I know that I can do git branch -a to see all the branches, but how would I pull all the branches locally so when I do git branch , it shows:

$ git branch
* master
* staging
* etc...

I've written my answer some time ago and last downvote motivated me to update it with my later experience :-)

Edit: previous version of this answer created branches with 'origin' prefix, all pointing to master branch, instead of actual branches, and having problems with variable expansions. This has been fixed as per comments.

You can fetch one branch from all remotes like this:

git fetch --all

fetch updates local copies of remote branches so this is always safe for your local branches BUT :

  • fetch will not update local branches (which track remote branches); If you want to update your local branches you still need to pull every branch.

  • fetch will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches: git branch -a

  • To update local branches which track remote branches:

    git pull --all
    

    However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all :

    git branch -r | grep -v '->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
    

    TL;DR version

    git branch -r | grep -v '->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
    git fetch --all
    git pull --all
    

    (it seems that pull fetches all branches from all remotes, but I always fetch first just to be sure)

    Run the first command only if there are remote branches on the server that aren't tracked by your local branches.

    PS AFAIK git fetch --all and git remote update are equivalent.



    Kamil Szot's comment, which 74 (at least) people found useful.

    I had to use:

    for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
    

    because your code created local branches named origin/branchname and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.


    To list remote branches:
    git branch -r

    You can check them out as local branches with:
    git checkout -b LocalName origin/remotebranchname


    You will need to create local branches tracking remote branches.

    Assuming that you've got only one remote called origin , this snippet will create local branches for all remote tracking ones:

    for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
    

    After that, git fetch --all will update all local copies of remote branches.

    Also, git pull --all will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.

    链接地址: http://www.djcxy.com/p/4436.html

    上一篇: 如何用git下载分支?

    下一篇: 如何获取所有的git分支?