Git: Clone from a branch other than master

GitGithubGithub for-Windows

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

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 - Git

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

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

Solution 4 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2510555View Question on Stackoverflow
Solution 1 - GitAguardienticoView Answer on Stackoverflow
Solution 2 - GitkamjaginView Answer on Stackoverflow
Solution 3 - GitBalog PalView Answer on Stackoverflow
Solution 4 - GitDenis SmirnovView Answer on Stackoverflow