Skip to content

Instantly share code, notes, and snippets.

@nuffin
Created June 1, 2024 14:16
Show Gist options
  • Save nuffin/6f95e4e874320a6876467144f506f61b to your computer and use it in GitHub Desktop.
Save nuffin/6f95e4e874320a6876467144f506f61b to your computer and use it in GitHub Desktop.
clonegit command for PowerShell, to clone git repositories in a structured directory
`@
Usage:
Clone-Git git-repo-url
Clone-Git -SshKeyFilePath C:\path\to\private-ssh-key git-repo-url
to clone the repository with a specified ssh key
add this function to $PROFILE file, and then re-open PowerShell to use it.
@`
function Clone-Git
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string] $SshKeyFilePath,
[Parameter(Mandatory=$false, Position=0)]
[string] $Url
)
Process
{
if (!$Url) {
Write-Error "Usage: ${0} [-SshKeyFilePath SSH-KEY-FILE-PATH] [email protected]:owner/project.git|https://gitserver.com/owner/project.git"
return
}
$PathItems = $Url -replace '.git$', '' -replace '(.*@|(https|http|ssh)://)([a-z0-9\.-]+)(:[0-9]+/|:|/)(.*)/(.*)', '$3 $5 $6' -split ' '
$Path = [string]::Join('\', $env:USERPROFILE, 'studio', $PathItems[0], $PathItems[1]);
$RepoName = $PathItems[2]
Write-Host "Cloning $Url into $Path/ as $RepoName ..."
New-Item -ItemType Directory $Path -ErrorAction SilentlyContinue
Push-Location $Path
if ($Url -match '^http' || !$SshKeyFilePath) {
git clone $Url
} else {
$SshKeyFilePath = $SshKeyFilePath -replace '^~', $env:USERPROFILE -replace '\\', '/'
# Write-Host $SshKeyFilePath
git clone -c core.sshCommand="ssh -i $SshKeyFilePath" $Url
}
Pop-Location
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment