0

Hi all I have written a PowerShell script to clone the repository to my local drive using PowerShell script.

Function CloneBitBucket
{
 [CmdletBinding()]
 param (
    [Parameter(Mandatory)]
    [string] $sourceLocation,
    [Parameter(Mandatory)]
    [string] $localPath 
)
 git clone $sourceLocation $localPath
}
CloneBitBucket

This is downloading all the available projects, but I would like to clone only a specific project instead of downloading all

Developer
  • 8,228
  • 37
  • 122
  • 230

1 Answers1

1

Projects per repository

It sounds like you might have multiple projects inside a single repository.

Both Git and Mercurial work much better when each project has its own repository, and neither has good support for cloning just a subdirectory. Consider the following answers.

  • Regarding Git:

    The idea is not to store everything in one giant git repo, but build a small repo as a main project, which will reference the right commits of other repos, each one representing a project or common component of its own.

  • Regarding Mercurial:

    you should create multiple repositories, one for each independent project as a start.

    Mercurial commits are made an a repository-wide level and you cannot checkout just a single subdirectory. This is unlike Subversion which allows you to make in-consistent commits by commiting only some files, but then it also allows you to checkout just a single subdirectory.

I advise you to split your multi-project repository up into multiple single-project repositories. It will save you a lot of headache later.

Sparse checkout with Git

However, it is possible check out just certain subdirectories with Git using something called sparse checkout. Note that with this approach your clone will still contain the entire directory tree. What changes is the files that are shown in your working copy.

Sparse checkout is a bit of a pain, and in my opinion not a good approach to handling multiple projects inside a single repository, but it's there if you want to use it.

Cloning a single branch with Git

If you wish to clone only a single branch with Git you can use the --single-branch and --branch options to git clone:

git clone --single-branch --branch some-branch $sourceLocation $localPath

I suspect can update your PowerShell function to support a third $branchName parameter.

Community
  • 1
  • 1
Chris
  • 112,704
  • 77
  • 249
  • 231
  • Hi `Chris` thanks for that, how can I download a branch from PowerShell – Developer Jul 26 '16 at 12:49
  • @Dotnet, are you using Git or Mercurial? – Chris Jul 26 '16 at 12:51
  • `Chris` I am using Git – Developer Jul 26 '16 at 13:31
  • I am confused with `--single-branch --branch some-branch` as you said if I pass $branchname as input then how the script will look like – Developer Jul 27 '16 at 09:29
  • `--single-branch --branch $branchname` – Chris Jul 27 '16 at 11:32
  • You mean to say `git clone $sourceLocation $localPath --single-branch --branch $branchname` – Developer Jul 27 '16 at 11:57
  • @Dotnet, I believe the last two arguments have to be the repository you're cloning (your `$sourceLocation`) and the directory you want to clone into (your `$localPath`), as I've shown in my answer. I have also linked to the official documentation for `git clone`, which you should probably look at. You can also try the command out to see if it does what you expect. – Chris Jul 27 '16 at 12:00