3922

Git clone will clone remote branch into local.

Is there any way to clone a specific branch by myself without switching branches on the remote repository?

masiboo
  • 4,537
  • 9
  • 75
  • 136
Scud
  • 41,923
  • 8
  • 26
  • 18

7 Answers7

8788
git clone -b <branch> <remote_repo>

Example:

git clone -b my-branch git@github.com:user/myproject.git

With Git 1.7.10 and later, add --single-branch to prevent fetching of all branches. Example, with OpenCV 2.4 branch:

git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git
Jorge E. Cardona
  • 92,161
  • 3
  • 37
  • 44
  • 578
    pierr: I'm not sure if this answers the description of the problem given above, but it *does* answer the actual question - how to clone a specific branch of a repository. I voted this up because it's the answer I was googling for when I came to this page. – Jaime Bellmyer May 15 '11 at 03:35
  • 29
    This works. It points the new HEAD at the specified branch rather than at the HEAD-branch in myproject. However, it still fetches *all* branches. See @edmar-miyake's answer. – cdunn2001 Mar 17 '12 at 20:35
  • 4
    It answers the description of the problem if you add a --depth X to the command. If you do so, it will clone only the specified branch and its last content. – ramsvidor Sep 12 '13 at 23:32
  • 9
    thx for `--single-branch`; git 2.5 is out at time of writing this. Don't care for older versions. – Bernhard Döbler Aug 20 '15 at 10:46
  • I used the above command, which did pull from github - but I pulled it into master, because that's the branch I was on - it did not create the branch I was cloning locally. That's what I was looking for. – zero_cool Nov 12 '15 at 19:30
  • we can also use : `git checkout -b ` `git pull origin ` , it works with me xD or, **simiply** `git fetch ` `git checkout ` – Abdallah Okasha Apr 10 '18 at 09:52
  • 3
    @jorge Why `-b` option requires a separate `--single-branch` flag? Does `-b` alone clones all branches? – CᴴᴀZ Jul 11 '18 at 06:23
  • @CᴴᴀZ Hi, the `--single-branch` IS NOT required. As you can see in the manpages, git clone "...creates remote-tracking branches for each branch in the cloned repository" and "--[no-]single-branch Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at." You save some time in big repos I guess. – Jorge E. Cardona Jul 11 '18 at 17:06
  • Thanks, for helping. `--single-braches` aren't required to work remind that <3 –  Jan 13 '19 at 03:04
  • This is especially helpful for scripting the installation of code that doesn't need to be built, for instance config files. I keep my NGINX configs in a repo and my userdata script clones and moves them into place with rsync. There is no need to `cd` into the repo if you cloned with HEAD pointing to the right branch. – Bruno Bronosky Feb 22 '19 at 22:57
  • @JorgeE.Cardona Was really helped me. I was taking the pull so it is taking from "master" branch which is not reflecting my latest changes of other updated sub-branch. – Dipak Jun 06 '20 at 17:25
  • @Dipak I'm happy it helped. I am unsure what is your question, the first command should include any children as well, but it sets the HEAD to the designed branch. Unless you are using `--single-branches` in which case you get only the commits leading to that branch and no other branches. Use `git log --graph --oneline --all` to see the whole tree. – Jorge E. Cardona Jun 07 '20 at 10:13
  • Is there a way to include branch in the ssh/https url? eg `git clone git@github.com:user/myproject.git/my-branch` – Marius Mar 17 '21 at 16:20
  • *NOTE* that `--single-branch` will cause future `git fetch` calls to only fetch that branch. You may find yourself later wondering why your tools don't show all the remote branches that you KNOW exist! Even after you fetch. – Flion Jun 23 '22 at 09:02
2426
git clone --single-branch --branch <branchname> <remote-repo>

The --single-branch option is valid from version 1.7.10 and later.

Please see also the other answer which many people prefer.

You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> url you're fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.

Again, that is not to say git clone --branch is not the way to accomplish that, it's just that it's not always what you want to accomplish, when you're asking about cloning a specific branch.

wim
  • 338,267
  • 99
  • 616
  • 750
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/191121/discussion-on-answer-by-michael-krelin-hacker-how-to-clone-a-specific-git-bran). – Jean-François Fabre Apr 02 '19 at 19:37
  • 2
    Pardon me, are you sure about the part you said "you're fetching all..."? I read somewhere that git fetch doesn't actually "copy" any files, it just fetches metadata and information about the changes. So it should be relatively light weight... Maybe you've used the word "fetch" literally and not from the git vocabulary? – aderchox Jul 16 '20 at 08:29
  • 3
    @aderchox, no it will actually fetch all the content. It is pretty smart about things it transfers when you update, but when you clone a big repository it actually pulls the history, unless you explicitly tell it not to. But it will still fetch the tip of the branch. What `git fetch` does not — it does not check out files, but that's not about the transfer. – Michael Krelin - hacker Jul 16 '20 at 13:07
  • 8
    Often you will also want `--depth 1` so that you only get the latest. This can save a lot of downloading time. – Eyal Feb 04 '21 at 03:57
  • `git clone -b branch_name --single-branch 'repo_url'` – Aditya Rajgor Mar 29 '22 at 15:37
351

Here is a really simple way to do it :)

Clone the repository

git clone <repository_url>

List all branches

git branch -a 

Checkout the branch that you want

git checkout <name_of_branch>
superlogical
  • 14,332
  • 9
  • 66
  • 76
  • 1
    This switched the working directory to the correct branch, but I'm not able to push any changes I make, because I'm not "currently on a branch". – Seppo Enarvi Jan 21 '15 at 09:46
  • 6
    This was the solution for me, since I had already cloned 'master'. I didn't know I could simply 'checkout' a remote branch. – yazzer Jul 30 '15 at 21:34
  • 2
    This is probably the correct way to do it; best-practices-wise – aaiezza Jul 25 '16 at 21:01
  • git checkout mybranchname then git fetch done – Shohanur Rahaman Mar 31 '17 at 16:54
  • 1
    This way doesn't clone only the choosen branch. This answer seems better: https://stackoverflow.com/a/7349740/3075243. For example if a repo has many branches that are big enough that we don't wanna clone each one. – Proustibat Jan 31 '18 at 23:04
  • 5
    Very crisp answer. One additional things you have to do is: After this step: "git checkout " Do this: git branch --set-upstream-to=origin/ Thanks. – ArunDhwaj IIITH Apr 11 '18 at 23:42
  • Won't this retain old files/folders from the original checkout that are no longer present in new branch? – ESR Oct 24 '18 at 01:42
  • For me it doesn't work. After clone when I run "git branch -a" it shows me nothing. Just blank space. – Atul Jan 24 '20 at 06:10
  • Not a good solution. By using `git clone ` you're getting ALL branches...! then switch to ``. – Yousha Aleayoub Sep 12 '20 at 18:31
  • 1
    If somebody creates new branch on github, how to fetch it ? – vikramvi Feb 18 '21 at 13:06
264

To clone a branch without fetching other branches:

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
Jaap
  • 3,081
  • 2
  • 29
  • 50
Edmar Miyake
  • 12,047
  • 3
  • 37
  • 38
  • 1
    Good solution. On older git (I have 1.5.5.6), a git branch --track $BRANCH origin/$BRANCH may be needed before the checkout. – Johannes Thoma Aug 13 '13 at 13:19
  • 2
    Works, and also fetches just those tags present on the branch, which is what I wanted. (I actually wanted to fetch _multiple_ branches, but only selected ones; for that, it sufficed to repeatedly `remote add` and `checkout` as here, then `git remote rm origin` to clean up.) – Jesse Glick Sep 03 '14 at 01:45
  • 2
    Perfect solution for shallowly incorporating a specific tag of a git repo in another project. Recommend omitting `-f` from the git remote command, then using `git fetch --depth=1 $BRANCH $TAG`, then `git checkout FETCH_HEAD`. The init is innocuous, and changing tags will automatically update the checked out code. – taranaki May 12 '17 at 20:41
  • Unlike Michael Krelin's (3-step) answer, this one actually worked for me (git 1.7.9.5) – Bobby Jack Dec 14 '18 at 13:11
  • after alot of fail, this code is works to me.. `git version 2.9.2` – Budi Mulyo Jan 24 '19 at 08:17
136

Use:

git checkout -b <branch-name> <origin/branch_name>

For example in my case:

 git branch -a
* master
  origin/HEAD
  origin/enum-account-number
  origin/master
  origin/rel_table_play
  origin/sugarfield_customer_number_show_c

So to create a new branch based on my enum-account-number branch, I do:

git checkout -b enum-account-number origin/enum-account-number

After you hit Return, the following happens:

Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dkinzer
  • 32,179
  • 12
  • 66
  • 85
  • 4
    Note that it may be useful to `git pull origin` first so that `git branch -a` can list all new (current) remote branches. – André Caron Oct 12 '12 at 04:13
  • 1
    Good point. Probably `git fetch` is better so that the auto merge doesn't happen, though. – dkinzer Oct 12 '12 at 14:09
  • this solution is helpful for creating a new local branch, switching to it and setting up to track the corresponding remote branch – Nissal Feb 20 '23 at 16:46
39

Create a branch on the local system with that name. e.g. say you want to get the branch named branch-05142011

git branch branch-05142011 origin/branch-05142011

It'll give you a message:

$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"

Now just checkout the branch like below and you have the code

git checkout branch-05142011
vvvvv
  • 25,404
  • 19
  • 49
  • 81
PlanetUnknown
  • 3,956
  • 4
  • 49
  • 67
34
git --branch <branchname> <url>

But Bash completion don't get this key: --branch

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
savgur
  • 365
  • 3
  • 2