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...
The bash for loop wasn't working for me, but this did exactly what I wanted. All the branches from my origin mirrored as the same name locally.
Edited: See Mike DuPont's comment below. I think I was trying to do this on a Jenkins Server which leaves it in detached head mode.
When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command
you can show all the branches of the repository, and with the command
you can then "download" them manually one at a time.
However, there is a much cleaner and quicker way, though it's a bit complicated. You need three steps to accomplish this:
First step
create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:
the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.
Second step
switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:
Third Step
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.
So now you can just type the command
git branch
and you can see that all the branches are downloaded.This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.
I wrote a little script to manage cloning a new repo and making local branches for all the remote branches.
You can find the latest version here:
To use it, just copy it into your git bin directory (for me, that’s
C:\Program Files (x86)\Git\bin\git-cloneall
), then, on the command line:It clones as usual, but creates local tracking branches for all remote branches.
We can put all branch or tag names in a temporary file, then do git pull for each name/tag:
For Windows users using PowerShell:
You will need to create local branches tracking remote branches.
Assuming that you've got only one remote called
origin
, this snippet will create local branches for all remote tracking ones:After that,
git fetch --all
will update all local copies of remote branches.Also,
git pull --all
will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.