65

I am trying to pull from a repository in Github. But I don't want to clone the master branch. I want to clone some other branch. When I try git clone <url>, I get the files from the master branch. What should I do?

Also, suppose the code is updated in the repository and I want to get the latest code, should I again use git clone ? Because the size of the project is huge. Also if I make changes to the project locally, and then I again use git clone, will the changes I made still be there? What if I don't want changes to be there?

I am not even sure if git clone is the right command. git pull or git fetch?

I am sorry, I am very new to git.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
user2510555
  • 967
  • 2
  • 8
  • 15

4 Answers4

75

Try this:

git init
git fetch url-to-repo branchname:refs/remotes/origin/branchname

EDIT

A better solution:

git clone -b mybranch --single-branch git://sub.domain.com/repo.git
Aguardientico
  • 7,641
  • 1
  • 33
  • 33
41
git clone <url>

clones and creates remote-tracking branches for each branch. If you want to see available branches (after cloning), you type

git branch -l

To switch to a particular branch after cloning you do:

git checkout <branchname>

where branchname is the name of the branch :)

If you want to clone and checkout a specific branch you do

git clone -b <branchname> <url>

The other commands you mention are for "updating" your current working copy. git pull gets all changes from the remote repository and merges them while git fetchonly gets them without merging.

kamjagin
  • 3,614
  • 1
  • 22
  • 24
  • 3
    Actually I think it is `git clone -b ` – kamjagin Jun 29 '13 at 18:23
  • 2
    Thanks for your answer :) But I can choose only one answer as the correct answer. I can't upvote your answer also because it requires at least 15 reputation points. – user2510555 Jun 29 '13 at 18:36
  • 2
    Luckily having the order as `git clone -b ` also works. This is good for requirements files. Where you might have `while read; do git clone $REPLY; done < requirements.txt` in a build script. Most entries in that file would just be URLs, but you can also add `-b ` to the end of any of them. – Bruno Bronosky Oct 19 '17 at 07:48
  • git clone -b , this solved my problem. – Yang Wang May 15 '18 at 22:59
  • I've never seen nor used `git branch -l` in my entire life before! I checked the man pages. It turns out that is the default whenever you call just `git branch`, which is what I've been doing my whole life. That `-l` really threw me off. From `man git branch`: `If --list is given, or if there are no non-option arguments, existing branches are listed`. – Gabriel Staples Aug 29 '20 at 16:06
  • Also, I think you mean to say to use `git branch -a`, *not* `git branch -l`. The `-a` option shows all branches, including the remote-tracking ones, whereas the `-l` option (or the lack of any options: just `git branch`) will not show remote-tracking branches at all, so you wouldn't be able to see what branches exist on github but not locally unless you use `git branch -r` to show remote branches, or `git branch -a` to show all branches. – Gabriel Staples Aug 29 '20 at 16:09
5

use git clone --branch <name> possibly adding --single-branch

as usual you have git clone --help to read details on commands

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
3

If you already cloned the repo and want to contribute to the branch other than master, do this:

$ git checkout --track origin/<branch-name>

Of course, be sure to specify the right name of the remote (origin in the example above)

This command will create a new local branch that will track a particular remote branch.