Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone all remote branches in Git

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.

like image 827
Peter Coulton Avatar asked Sep 15 '08 22:09

Peter Coulton


People also ask

How do I clone a git repository with all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.

Does git clone get all remote branches?

git clone downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master".

How do I clone a remote branch?

In order to clone a specific branch, you have to execute “git branch” with the “-b” and specify the branch you want to clone. $ git clone -b dev https://github.com/username/project.git Cloning into 'project'... remote: Enumerating objects: 813, done.


2 Answers

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 which is done automatically by:

$ git checkout experimental 

and you will see

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.

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 & 
like image 86
emk Avatar answered Oct 15 '22 23:10

emk


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

  • bigfish's answer
  • Dave's answer
like image 39
Gabe Kopley Avatar answered Oct 15 '22 23:10

Gabe Kopley