Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Last active November 13, 2017 10:08
Show Gist options
  • Save dazfuller/1a5f84ca1b86f3b84b64 to your computer and use it in GitHub Desktop.
Save dazfuller/1a5f84ca1b86f3b84b64 to your computer and use it in GitHub Desktop.
Update a cloned repository to track all remote branches
param (
[Parameter(Mandatory=$true, HelpMessage="Location of the Git repository to update")]
[string]$SourceLocation
)
# Test the source code location parameter
if ((Test-Path -Path $SourceLocation -PathType Container) -eq $false) {
Write-Host "The specified source code location is not valid" -ForegroundColor Red
exit 1
}
$SourceLocation = (Get-Item $SourceLocation).FullName
Push-Location $SourceLocation
$currentBranch = git rev-parse --abbrev-ref HEAD
$branches = git branch -a
$remotes = $branches | Where-Object {$_.Contains("remotes/") -and $_.Contains("HEAD") -eq $false}
foreach ($remote in $remotes) {
$remote = [System.Collections.ArrayList]($remote.Trim().Split("/"))
$remote.RemoveAt(0)
$source = $remote[0]
$branch = $remote[$remote.Count - 1]
$remote = $remote -join '/'
$test = git branch --list $branch
if ($test -eq $null) {
git branch --track $branch $remote
}
Write-Host "Updating $($branch) from $($source)" -ForegroundColor Green
if ($branch.CompareTo($currentBranch) -eq 0) {
git fetch $source $branch
} else {
git fetch $source $branch`:$branch
}
}
Pop-Location
@dazfuller
Copy link
Author

Created this so that you can have a local repo which shadows a remote, allowing all branches to be updated from the branch they're tracking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment