Created
November 21, 2019 20:15
-
-
Save paulmallon/3da3436d33ca3b9f447458e8d997bf4a to your computer and use it in GitHub Desktop.
Powershell function to clone a git repository with tracking of all remote branches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-GitRepo { | |
param( | |
[Parameter(Position=0, Mandatory)] | |
$url, | |
[Parameter()] | |
[switch] | |
$DoNotTrackRemoteBranches | |
) | |
$name = (split-path -leaf $url).replace(".git","") | |
$path = Join-Path (Get-Location) $name | |
write-host "Cloning '$name' into '$path'...." -NoNewline | |
git clone $url $path -q | |
if($LASTEXITCODE -ne 0 ) {exit;} | |
write-host "Done!" | |
if($DoNotTrackRemoteBranches.IsPresent) { | |
return | |
} | |
$currentBranch = git -C $path rev-parse --abbrev-ref HEAD | |
git -C $path branch -r | % { | |
if( $_ -match "^\s*([0-9a-zA-Z\/_.-]*)$") { | |
$branch = $_.replace("origin/","").Trim(); | |
if($branch -ne $currentBranch) { | |
git -C $path branch --track $branch origin/$branch | |
} else { | |
write-host "Branch '$currentBranch' set up to track remote branch '$branch' from 'origin'." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment