4

I am trying to clone a repo from an SSH remote like this:

git clone "ssh://user@project.example.com/var/www/git/www"

This works OK but using this command I am actually cloning the "master" branch of the repo, but instead I want to clone another branch which is called "dev2".

How do I achieve that?

Nicolas Kaiser
  • 1,628
  • 2
  • 14
  • 26
SwissChocolate
  • 237
  • 2
  • 4
  • 15

2 Answers2

5

after git clone.

you can just

git branch --track dev2 origin/dev2
git checkout dev2

to change your branch to dev2 easily.

or a short cut

git clone -b dev2 "ssh://user@project.example.com/var/www/git/www"
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • You will probably have to create a local branch first: `git branch --track dev2 origin/dev2` – user229044 Aug 10 '11 at 06:11
  • Your answer now creates the branch but doesn't check it out. – user229044 Aug 10 '11 at 06:24
  • Thank you for your answer. but I want to clone a certain branch of a repo, I dont need to clone the entire repo, would that be possible? – SwissChocolate Aug 10 '11 at 15:55
  • `git clone -b "ssh://user@project.example.com/var/www/git/www"` – TheOneTeam Aug 10 '11 at 16:03
  • @KitHo: this will only checkout the branch after clone, it will still clone all branches – knittl Aug 10 '11 at 16:36
  • @knittl: the git will clone all the branches from the repository. It increases the flexibility of switching different branches without asking the remote repo again. – TheOneTeam Aug 10 '11 at 16:38
  • @KitHo: I know, I've been a git user for quite some time now :) the OP wanted to know in his comment how to clone a single branch only – knittl Aug 10 '11 at 16:39
  • @Kit Ho: Thank you very much. Your answer in the comments works for me so would be great if you can please update your main reply so I can accept it as an answer :) – SwissChocolate Aug 12 '11 at 00:17
3

with git you generally clone complete repositories (all branches). if you want to clone only a single branch and never get any other branches use the following commands:

git init project
cd project
git remote add -f -t dev2 origin 'ssh://user@project.example.com/var/www/git/www'
git checkout -b dev2 origin/dev2
knittl
  • 246,190
  • 53
  • 318
  • 364