1

I am switching from SVN to Bitbucket, in SVN I have HTTP URL of the branch and I could check out just that branch.

I see that in case of Bitbucket things are bit different, by cloning the repo git clone <<<repo_url>>> I can check out the master branch of that repo and then later switch to any of another branch in that repo using git checkout <<branch name>>.

But I do not want this redundant step of cloning the repo and checking out master, I directly want to check out the non-master branch I need from that repo.

But so far I cannot find any solution, and I cannot clone that branch because the clone URL which I get from the Bitbucket is of the repo.

Do anyone knows how to check out the remote non-master Bitbucket branch without checking out the master?


It is not duplicate of this because as per answer of the question you have mentioned, in order to execute git clone <url> --branch <branch> --single-branch [<folder>] I need to be connected/configured with that repo, which means a pre-cloning. My question is about checking out a non-master branch without checking our / cloning the master,
pjj
  • 2,015
  • 1
  • 17
  • 42
  • 1
    You do know this is entirely what distributed version control was _not_ intended for? – xtofl Dec 28 '17 at 14:38
  • 2
    Possible duplicate of [How to clone a single branch in git?](https://stackoverflow.com/questions/1778088/how-to-clone-a-single-branch-in-git) – xtofl Dec 28 '17 at 14:39
  • @xtofl I don't think it is duplicate because as per answer of the question you have mentioned, in order to execute `git clone --branch --single-branch []` I need to be connected/configured with that repo, which means a pre-cloning. My question is about checking out a non-master branch without checking our / cloning the master, – pjj Dec 28 '17 at 14:44
  • I do not understand why `git clone --branch --single-branch []` won't work for you. You do not need to "preclone" the repo for this to work. You just do this instead of the typical `git clone `. If you are required to clone the repo before hand, you **have** to clone a branch. You can take the default `master` or use the `--branch` option to specify which branch. – Code-Apprentice Dec 29 '17 at 02:45

2 Answers2

4

But I do not want this redundant step of cloning the repo and checking out master, I directly want to check out the non-master branch I need from that repo.

Then you can do

git clone <url> --branch <branch> --single-branch [<folder>]

as stated in the answer that you linked.

It is not duplicate of this because as per answer of the question you have mentioned, in order to execute git clone <url> --branch <branch> --single-branch [<folder>] I need to be connected/configured with that repo, which means a pre-cloning.

No, there is no "pre-cloneing" required. The command you cite does the cloning just like the bare git clone command does. The difference is that the --branch allows you to specify which branch to start with, just like you are asking.

Nor is any previous "connection" to the remote repo required. Specifying the URL in the command creates the connection.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you for your inputs. Just to confirm from you - that checkout branch would be remote trackable, right? – pjj Dec 29 '17 at 12:31
  • @pjj Yes, the clone should set up the remote tracking automatically. If it doesn't, it is not difficult to manually set it yourself. – Code-Apprentice Dec 29 '17 at 14:49
2

It’s caused by the difference between svn and git.

  • For svn repo, all the branches existing in the same working directory, so you can get the branch through URL by http://svn-repo/branchname.
  • While for git repo, all the branches existing parallely. That means working directoty changes differently based on different branches. When HEAD is pointing to a certain branch, the working directory will only contains files belong to the branch. So you can not add the branch name after repo URL (as svn), but need to checkout the branch to the working directory.

And for git clone <repo URL>, the HEAD is point to default (main) branch by default. And for most cases, the default (main) branch is master branch.

If you want the HEAD point to another branch immediately after cloning the git repo (without git checkout command), you can change the default (main) branch.

Such as if you want to change the main branch from master to o1, you can set in bitbucket as below:

In the bitbucket repo -> settings -> Repository details -> select the Main branch as o1 -> Save repository details.

enter image description here

Then you can also find in your bitbucket repo branches, the o1 branch is main branch now.

enter image description here

Now when you clone the repo, the current branch is always o1 branch.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74