Git/Rebase-CountryBranches.ps1

#find all remote branches for countries
$RemoteCountryBranches = Get-GitBranches | ? {$_.Contains('remotes/origin/country')}

foreach ($RemoteCountryBranch in $RemoteCountryBranches) {
    #find the local branch tracking the remote branch
    $RemoteCountryBranch = $RemoteCountryBranch.Substring($RemoteCountryBranch.IndexOf('origin'))
    $LocalCountryBranch = git branch -vv | ? {$_.Contains(('{0}' -f $RemoteCountryBranch))}

    #if one exists then check it out and pull any available commits
    if ($LocalCountryBranch -ne '') {
        $LocalCountryBranch = $LocalCountryBranch.TrimStart(' ')
        $LocalCountryBranch = $LocalCountryBranch.Substring(0,$LocalCountryBranch.IndexOf(' '))
        git checkout $LocalCountryBranch
        git pull --ff-only
    }
    #if one doesn't exist then create one by checking out the remote branch and tracking it
    else {
        git checkout $RemoteCountryBranch -t    
    }

    #rebase onto master and force push to origin
    git rebase master
    git push -f
}