Last Updated: March 02, 2016
· turg0n

git clone all remote branches locally

Whenever you clone a repo, you do not clone all of its branches by default.

If you wish to do so, use the following script:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch

This is roughly based on a solution found here

Hope this comes in handy! :)

Say Thanks

11 Responses
Add your response


This isn't quite true. You clone the entire repo, including all remote branches. You do not create local tracking branches for said branches but this really doesn't buy you much. Recent version of git still allow you to check them out and they will automatically create the local tracking branch when you do that (just check it out without the remote name like origin/)

over 1 year ago ·
Foto perfil

great :)

over 1 year ago ·

@aaronjensen so, is there another way than just doing a git clone ? :)

over 1 year ago ·

@turg0n I'm not sure I understand your question. git clone is sufficient for me. My preference is only to have local tracking branches for branches I'm actually working with. Anything more is just noise.

As I said, when I want a local tracking branch for the foo branch I just git checkout foo and git is smart enough to automatically create a tracking branch based on origin/foo.

Maybe you could say why you find this useful?

over 1 year ago ·

@aaronjensen Ahh I see. Well here's my use case. I deleted my local repository. I wanted to check out the remote one on github, but I also wanted to see all the branches locally, because I happened to also be working on all of them (more or less).

In your case, you are correct, you don't need to clone all of them!

So, this is only useful if you need to have all remote branches locally.

over 1 year ago ·

@turg0n Gotcha. To be clear, when you clone you do have all your remote branches locally. Any git UI would show them under the origin remote. Literally all creating a local branch does is create a new file in your .git folder that points to the same commit that the remote branch does. Remember, with git, you have the entire repository on your machine when you clone. There isn't really a notion of cloning remote branches.

over 1 year ago ·

@aaronjensen well, as you probably figured out already.. I'm using the term ;) That's why git branch did not show me all of the branches !! Haha. Thank you for taking the time to answer mate!

over 1 year ago ·

@turg0n git branch -a

over 1 year ago ·

Upon this I created a bash script for fetching all branches and tags of a git project as snapshots into separate folders


over 1 year ago ·
2013 09 21 18.45.23

I needed to do this but I'm in Windows using PowerShell, so I updated my PowerShell script to do something like this (just not as elegantly).


Thanks btw, this was really helpful in helping me figure this out

over 1 year ago ·


while read trackingbranch; do
git branch --track ${trackingbranch#bremotes/origin/} $trackingbranch
done< <(grep -v 'HEAD|master'< <(git branch -a)|grep remotes)

over 1 year ago ·
Filed Under