Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+7 votes
3 views
in DevOps and Agile by (29.3k points)
edited by

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

1 Answer

+7 votes
by (50.2k points)
edited by

git fetch --all and git pull -all will only track the remote branches and track local branches that track remote branches respectively.

But it is not sufficient to track all remote branches

For tracking all remote branches run this command

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

Run this command only if there are remote branches on the server which are untracked by your local branches.

Then after this command run 

git fetch --all

git pull --all

Thus, you can fetch all git branches.

On a side note: some will suggest you with

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

But this will create a local branch named origin/branchname which will give you "refname 'origin/branchname' is ambiguous whenever you referred to it.

For more information regarding Git commands please go through the following tutorial link:

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...