Git: Clone from a branch other than master
Asked Answered
E

4

67

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.

Excitability answered 29/6, 2013 at 18:6 Comment(1)
#1778588Pippo
C
79

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
Charlettecharley answered 29/6, 2013 at 18:9 Comment(8)
I did not get the part after url-to-repo. Should I just mention the branchname? For example- git fetch <url> demo (assuming dev is the branch name?)Excitability
I've changed for a cleaner solutionCharlettecharley
BTW for the first solution: where it says "branchname" it should said the branch that you want to clone something like git fetch my_repo_url demo:refs/remotes/origin/demoCharlettecharley
Okay, thanks. I tried git branch -l and got all the branches. I am trying the second solution.Excitability
what does '--single-branch' do?Melina
Sweet! This saved me a ton of time. I had trouble with another clone on my machine. I brought a branch from that clone into my new clone. Et voila!Bijugate
@ user3804598 type this git clone [<options>] [--] <repo> [<dir>] --single-branch clone only one branch, HEAD or --branchPippo
#1778588Pippo
A
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.

Apathetic answered 29/6, 2013 at 18:11 Comment(6)
Actually I think it is git clone -b <branchname> <url>Apathetic
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.Excitability
Luckily having the order as git clone <url> -b <branchname> 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 <branchname> to the end of any of them.Bink
git clone -b <branchname> <url>, this solved my problem.Snafu
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.Cabinetmaker
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.Cabinetmaker
R
6

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

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

Renita answered 29/6, 2013 at 18:11 Comment(0)
C
4

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.

Cacia answered 22/12, 2020 at 4:11 Comment(1)
What is the behavior if the local branch name already exists? Does it throw an error? Or does it just elegantly switch to that branch?Shulman

© 2022 - 2024 — McMap. All rights reserved.