Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch all Git branches

I cloned a Git repository, which contains about five 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 the following?

$ git branch * master * staging * etc... 
like image 851
David542 Avatar asked Apr 25 '12 09:04

David542


People also ask

How do I fetch all branches in GitHub?

So now you can just type the command git branch and you can see that all the branches are downloaded. This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.

Does git pull pull all branches?

git pull fetches updates for all local branches, which track remote branches, and then merges the current branch.

How do I pull everything from git?

One thing to note is that by default, git fetch will only bring you changes from the current branch. To get all the changes from all the branches, use git fetch --all . And if you'd like to clean up some of the branches that no longer exist in the remote repository, git fetch --all --prune will do the cleaning up!


2 Answers

TL;DR answer

git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | 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.

Complete answer

You can fetch all branches from all remotes like this:

git fetch --all 

It's basically a power move.

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

  1. 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.

  2. 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 '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done 

P.S. AFAIK git fetch --all and git remote update are equivalent.



Kamil Szot's comment, which folks have 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.

like image 108
Wookie88 Avatar answered Sep 24 '22 18:09

Wookie88


To list remote branches:

git branch -r 

You can check them out as local branches with:

git checkout -b LocalName origin/remotebranchname 
like image 38
Learath2 Avatar answered Sep 21 '22 18:09

Learath2