Developer
Developer

Reputation: 8646

Clone a specific source from Bitbucket repository powershell

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

Upvotes: 0

Views: 2589

Answers (1)

Chris
Chris

Reputation: 137109

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.

Upvotes: 1

Related Questions