0ypmka
Last Updated: March 02, 2016
·
36.53K
· turg0n
Pic

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:

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

This is roughly based on a solution found here

Hope this comes in handy! :)

Say Thanks
Respond

11 Responses
Add your response

1895
Cd93483c1bb616efb5480b639dbb9ca3

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 ·
1901
Foto perfil

great :)

over 1 year ago ·
1914
Pic

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

over 1 year ago ·
1915
Cd93483c1bb616efb5480b639dbb9ca3

@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 ·
1918
Pic

@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 ·
1919
Cd93483c1bb616efb5480b639dbb9ca3

@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 ·
1921
Pic

@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 ·
1924
Cd93483c1bb616efb5480b639dbb9ca3

@turg0n git branch -a

over 1 year ago ·
11043
Aba5754b65d14005d2a06d1878bbcaea

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

https://gist.github.com/hfossli/7562257

over 1 year ago ·
15541
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).

https://gist.github.com/dazfuller/1a5f84ca1b86f3b84b64

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

over 1 year ago ·
22655
None

!/bin/bash

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