How do I clone all remote branches with Git?
38
I have a master and a development branch, both pushed to GitHub. I've cloned, pulled, and fetched, but I remain unable to get anything other than the master branch back.

I'm sure I'm missing something obvious, but I have read the manual and I'm getting no joy at all.
improve this question | comment
Russell Hane Created at: 2013-11-13 17:07:43 UTC By Russell Hane
The accepted answer here (git branch -a) shows you the branches in the remote, but if you attempt to check any of those out you will be in a 'detached HEAD' state. The next answer down (second most upvotes) answers a different question (to wit: how to pull all branches, and, again, this only works for those you're tracking locally). Several of the comments point out that you could parse the git branch -a results with a shell script that would locally track all the remote branches. Summary: There's no git native way to do what you want and it might not be all that great an idea anyway. - Irma Wuckert
Maybe just copy the entire folder the old fashioned way? scp some_user@example.com:/home/some_user/project_folder ~ Not sure if that solution works for github though.. - Maya Gaylord
17 Answers
0
I'm going to add my 2 cents here because I got here trying to find out how to pull down a remote branch I had deleted locally. Origin was not mine, and I didn't want to go through the hassle of re-cloning everything 

This worked for me:

assuming you need to recreate the branch locally:

git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name


So if I forked from gituser/master to sjp and then branched it to sjp/mynewbranch it would look like this:

$ git checkout -b mynewbranch
$ git branch -a
  master
  remotes/sjp/master
  remotes/sjp/mynewbranch
$ git fetch (habit to always do before)
$ git rebase remotes/sjp/mynewbranch

0
Just do this:

$ git clone git://example.com/myproject
$ cd myproject
$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'
$ git pull
Already up-to-date.
$ git branch
* branchxyz
  master
$ git branch -a
* branchxyz
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branchxyz
  remotes/origin/branch123


You see, 'git clone git://example.com/myprojectt' fetches everything, even the branches, you just have to checkout them, then your local branch will be created.
0
This Bash script helped me out:

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


It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a 

git fetch --all
git pull --all


to be sure.
0
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax.  You can just do:

git checkout somebranch


Git will automatically do the right thing:

$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'


Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 1.8.2.1:


  If <branch> is not found but there does exist a tracking branch in
  exactly one remote (call it <remote>) with a matching name, treat as
  equivalent to

$ git checkout -b <branch> --track <remote>/<branch>


0
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.

To create a local branch based on a remote branch, do something like:

git checkout -b dev refs/remotes/origin/dev

Which should return something like:

Branch dev set up to track remote branch refs/remotes/origin/dev.
Switched to a new branch "dev"

Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch.  Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
0
Using the --mirror option seems to copy the remote tracking branches properly.
However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.

git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false

0
Regarding,


  $ git checkout -b experimental origin/experimental


using

$ git checkout -t origin/experimental


or the more verbose but easier to remember

$ git checkout --track origin/experimental


might be better, in terms of tracking a remote repository. 
0
When you do "git clone git://location", all branches and tags are fetched.

In order to work on top of a specific remote branch, assuming it's the origin remote:

git checkout -b branch origin/branchname

0
If you have many remote branches that you want to fetch at once, do:

$ git remote update
$ git pull --all


Now you can checkout any branch as you need to, without hitting the remote repository.
0
Use my tool git_remote_branch (you need Ruby installed on your machine). It's built specifically to make remote branch manipulations dead easy.

Each time it does an operation on your behalf, it prints it in red at the console. Over time, they finally stick into your brain :-)

If you don't want grb to run commands on your behalf, just use the 'explain' feature. The commands will be printed to your console instead of executed for you.

Finally, all commands have aliases, to make memorization easier.

Note that this is alpha software ;-)

Here's the help when you run grb help:


git_remote_branch version 0.2.6

  Usage:

  grb create branch_name [origin_server] 

  grb publish branch_name [origin_server] 

  grb rename branch_name [origin_server] 

  grb delete branch_name [origin_server] 

  grb track branch_name [origin_server] 



  Notes:
  - If origin_server is not specified, the name 'origin' is assumed 
    (git's default)
  - The rename functionality renames the current branch

  The explain meta-command: you can also prepend any command with the 
keyword 'explain'. Instead of executing the command, git_remote_branch 
will simply output the list of commands you need to run to accomplish 
that goal.

  Example: 
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove, rm
  publish: publish, remotize
  rename: rename, rn, mv, move
  track: track, follow, grab, fetch

0
A git clone is supposed to copy the entire repository.  Try cloning it, and then run git branch with no additional arguments.  It should list all the branches.  If then you want to switch to branch "foo" instead of "master", use git checkout foo.
0
I needed to do exactly the same. Here is my Ruby script.

#!/usr/bin/env ruby

local = []
remote = {}

# Prepare
%x[git reset --hard HEAD]
%x[git checkout master] # Makes sure that * is on master.
%x[git branch -a].each_line do |line|
  line.strip!
  if /origin\//.match(line)
     remote[line.gsub(/origin\//, '')] = line
   else
     local << line
   end
end
# Update 
remote.each_pair do |loc, rem|
  next if local.include?(loc)
  %x[git checkout --track -b #{loc} #{rem}]
end
%x[git fetch]

0
Use aliases. Though there aren't any native Git one-liners, you can define your own as

git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'


and then use it as

git clone-branches

0
Better late than never, but here is the best way to do this:

mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config unset core.bare
git reset --hard


At this point you have a complete copy of the remote repo with all of it's branches (verify with git branch).  You can use --mirror instead of --bare if your remote repo has remotes of its own.
0
A little late to the party, but I think this does the trick:

mkdir YourRepo
cd YourRepo
git init --bare .git                       # create a bare repo
git remote add origin REMOTE_URL           # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/*   # fetch tags
git init                                   # reinit work tree
git checkout master                        # checkout a branch


If this does something undesirable, I'd love to know. However, so far, this works for me.
0
As posted by Jacob Fike:


  Better late than never, but here is the best way to do this:

mkdir repo 
cd repo 
git clone --bare path/to/repo.git .git 
git config unset core.bare 
git reset --hard 

  
  At this point you have a complete copy of the remote repo with all of it's branches
  (verify with git branch). You can use --mirror instead of --bare if your remote repo
  has remotes of its own.


I found this worked well for me, giving me a local branch for every branch in the remote, however I found that I needed "--" before the "unset" option of git config.

(Except of course the 'drawback' of doing it this way is that you then have no remote tracking branches in your repository, only local braches, so although you get the branches I'm not sure how well a merge back would work).
0
First, clone a remote Git repository and cd into it:

$ git clone git://example.com/myproject
$ cd myproject


Next, look at the local branches in your repository:

$ git branch
* master


But there are other branches hiding in your repository! You can see these using the -a flag:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental


If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental


But if you want to work on that branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental


Now, if you look at your local branches, this is what you'll see:

$ git branch
* experimental
  master


You can actually track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets


At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &

Your Answer