Cloning all remote branches with Git

 
Published on 2013-09-27 by John Collins.

The process to get all of the branches in your Git repository checked out locally should be a simple task, right?

Wrong.

According to the accepted and most popular answer to this question on Stackoverflow (about as authoritative as Wikipedia these days), you need to run a shell script to achieve this:

How to fetch all git branches: top answer

It gets worse, just look at some of the answers in another related question here, everything from multiple Git commands, shell scripts, and Ruby scripts wrapping the Git CLI:

How do I clone all remote branches with Git?

It boils down to the following five commands:

mkdir myrepo
cd myrepo
git clone --bare user@hostname:/path/to/myrepo.git .git 
git config core.bare false
git reset --hard

Now when you run git branch, you should see all of the branches checked out locally.

............................................________........................ 
....................................,.-‘”...................``~.,.................. 
.............................,.-”...................................“-.,............ 
.........................,/...............................................”:,........ 
.....................,?......................................................\,..... 
.................../...........................................................,}.... 
................./......................................................,:`^`..}.... 
.............../...................................................,:”........./..... 
..............?.....__.........................................:`.........../..... 
............./__.(.....“~-,_..............................,:`........../........ 
.........../(_....”~,_........“~,_....................,:`........_/........... 
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}........... 
...........((.....*~_.......”=-._......“;,,./`..../”............../............ 
...,,,___.\`~,......“~.,....................`.....}............../............. 
............(....`=-,,.......`........................(......;_,,-”............... 
............/.`~,......`-...............................\....../\................... 
.............\`~.*-,.....................................|,./.....\,__........... 
,,_..........}.>-._\...................................|..............`=~-,.... 
.....`=~-,_\_......`\,.................................\........................ 
...................`=~-,,.\,...............................\....................... 
................................`:,,...........................`\..............__.. 
.....................................`=-,...................,%`>--==``....... 
........................................_\..........._,-%.......`\............... 
...................................,<`.._|_,-&``................`\..............

Updated 2023 : note that the above post was originally published in 2013 and may be outdated, but is left here for archival purposes. I have fixed some broken links above.