Skip to content

Instantly share code, notes, and snippets.

@paulmallon
Created November 21, 2019 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulmallon/3da3436d33ca3b9f447458e8d997bf4a to your computer and use it in GitHub Desktop.
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.
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