Categories
git git-branch git-clone remote-branch

How do I clone all remote branches?

4586

My master and development branches are tracked remotely on GitHub. How do I clone both these branches?

13

  • 157

    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.

    Jun 18, 2012 at 22:43

  • 6

    Maybe just copy the entire folder the old fashioned way? scp [email protected]:/home/some_user/project_folder ~ Not sure if that solution works for github though..

    Sep 26, 2012 at 22:51

  • 24

    Rather than saying “I’ve cloned, pulled, and fetched,” much better to show us the exact commands that you executed.

    Nov 22, 2013 at 18:17

  • 85

    It always boggles me why “clone” isn’t in the sense of an exact copy. If it’s an exact clone, shouldn’t all the branches be part of the local repository? I mean isn’t that one of the point of being distributed? So when something repository is gone you still have a complete copy of everything. Or is it the so called “remote” really are part of the local repository already?

    – huggie

    Jul 11, 2016 at 6:31


  • 33

    Seeing all the upvotes, answers, comments on answers and the mind-boggling number of views, I think it is time git added a command for doing this. And right you are @huggie, my thoughts exactly.

    Aug 29, 2016 at 4:29

4998

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! See these using the -a flag:

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

To take a quick peek at an upstream branch, check it out directly:

$ git checkout origin/experimental

To work on that branch, create a local tracking branch, which is done automatically by:

$ git checkout experimental

Branch experimental set up to track remote branch experimental from origin.
Switched to a new branch 'experimental'

Here, “new branch” simply means that the branch is taken from the index and created locally for you. As the previous line tells you, the branch is being set up to track the remote branch, which usually means the origin/branch_name branch.

Your local branches should now show:

$ git branch
* experimental
  master

You can 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 &

28

  • 140

    How can someone create automatically all the remote branches, e.g. experimental for origin/experimental?

    Jun 4, 2009 at 16:33

  • 60

    Cristian: I used to always create a branch ‘foo’ for every branch ‘origin/foo’, but this led to two problems: (1) I wound up with lots of really stale tracking branches that were many commits behind the corresponding remote branch, and (2) in older versions of git, running ‘git push’ would attempt to push all my local branches to a remote, even when those branches were stale. So now I only keep local branches for things that I’m actively developing, and access the origin/* branches directly if I need information about them. (That said, you could use a shell script to parse ‘git branch -a’.)

    – emk

    Jul 20, 2009 at 21:44

  • 51

    “git fetch <origin-name> <branch-name>” brings the branch down locally for you.

    – user67627

    Feb 7, 2010 at 17:10

  • 163

    Good answer, but kinda misses the question. I was looking for a one-liner to checkout all the remote branches.

    – cmcginty

    Oct 19, 2010 at 21:01


  • 35

    The question was about cloning all remote branches, not checking them out. And, as I noted above, you really don’t want to make any more local tracking branches than necessary, because when they get really stale, they can cause headaches.

    – emk

    Oct 28, 2010 at 12:43

908

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

git pull --all

Now you can checkout any branch as you need to, without hitting the remote repository.


Note: This will not create working copies of any non-checked out branches, which is what the question was asking. For that, see

15

  • 13

    If I do git clone, I have the master branch locally and 10 branches “remote”. So THIS answer by Gabe was very helpful and answers the question.

    – basZero

    Jan 27, 2012 at 19:07

  • 52

    this only fetch remote branches that have been locally added not any remote branch

    – jujule

    Feb 10, 2012 at 11:45

  • 35

    The first command is redundant. Simply git pull --all will do the same – it just won’t fetch twice. And infosec812 is right that this does not answer the question anyway. I wonder how this got so many upvotes.

    Apr 6, 2012 at 14:03


  • 57

    This is not helpful at all, doesn’t pull any remote branches other than that is existing.

    – Avinash R

    Apr 11, 2013 at 15:02

  • 11

    It’s not the first time when an answer that is simply wrong because it doesn’t answer the question gets hundreds of upvotes from people who either didn’t understood questions and/or answer, or landed by google for searching something other as the original OPs problem and such ‘answer’ have accidentally answered their problem. Unfortunately there’s no valid flag for answer that do not answer the question, the only way is downvoting, which obviously doesn’t work in some cases, so such noise is about to stay forever 🙁

    Apr 10, 2018 at 14:43

529

This Bash script helped me out:

#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|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.

One liner: git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
As usual: test in your setup before copying rm -rf universe as we know it

Credits for one-liner go to user cfi

17

  • 25

    This is really close to being a perfect solution.. The only thing that would make it better is if this functionality were built-in as an option in git.

    Oct 27, 2011 at 17:15

  • 61

    “One liner”: git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs As usual: test in your setup before copying rm -rf universe as we know it

    – cfi

    Sep 18, 2012 at 12:38


  • 5

    This command creates the feature branches from remote as normal branches (not feature branches) – how to fix this?

    – Alex2php

    Mar 20, 2014 at 14:31

  • 5

    if you run into issues with “/” in branch names there is a solution below using a git alias. see answer by “nobody” on “answered May 15 ’13 at 11:02”

    – wemu

    Jan 12, 2015 at 11:54

  • 13

    I’m trimming just remotes/origin/ to preserve namespaces: for BRANCH in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do git branch --track "${BRANCH#remotes/origin/}" "${BRANCH}"; done

    – kgadek

    Jul 2, 2015 at 8:33