Cloning all remote branches with Git

Looking at the various options for cloning all remote branches to your local Git repository.

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.

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

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the five.today personal productivity app. I love open source, technology, and economics.