Git: Clone from a branch other than master

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.


Solution 1:

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

Solution 2:

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.

Solution 3:

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

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