tags:

views:

38785

answers:

7

I have a master and a dev branch, both pushed to github, I've cloned, pulled, 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 RTM any I'm getting no joy at all.

A: 

A git-clone is supposed to copy the entire repository. Try cloning it, and then run "git-branch" with no arguments. It should list all the branches. If then you want to switch to branch "foo" instead of "master, use "git-checkout foo".

MattoxBeckman
without the "-" :)
elmarco
You can run git commands with or without the hyphen. Both "git-branch" and "git branch" will work.
Peter Boughton
@Peter: not with version >= 1.6 of git, afaik
elmarco
+4  A: 

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
elmarco
+12  A: 

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.

Luuk Paulussen
You don't need refs/remotes here. git checkout -b dev origin/dev will work fine.
emk
+184  A: 

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
  origin/HEAD
  origin/master
  origin/v1.0-stable
  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
  master
* experimental

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
  origin/HEAD
  origin/master
  origin/v1.0-stable
  origin/experimental
  win32/master
  win32/new-widgets

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

$ gitk --all &
emk
How can someone create automatically all the remote branches, e.g. experimental for origin/experimental?
Cristian Ciupitu
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
thank you for this answer, helped me out :)
viatropos
"git fetch <origin-name> <branch-name>" brings the branch down locally for you.
Orange Box
Yes, it helped me out too. Thanks!
Rimian
Good answer, but kinda misses the question. I was looking for a one-liner to checkout all the remote branches.
Casey
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
+3  A: 

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
webmat
+3  A: 

$ git checkout -b experimental origin/experimental

use

$ git checkout -t origin/experimental

might be better, in terms of tracking remote repository.

murphytalk
A: 

thank you, that works! use

$ git branch -a

to view all branches (not only active)

Anton
`git branch -a` is already mentioned in the [accepted answer](http://stackoverflow.com/questions/67699/how-do-i-clone-all-remote-branches-with-git/72156#72156) (you could have also commented on it if you had a reputation > 50).
Cristian Ciupitu