Git/Rebase-CountryBranches.ps1

function Rebase-CountryBranches {
    param(
        # Commit/branch to rebase the country branches onto
        [Parameter(Mandatory=$false)]
        [string]
        $TargetCommit = 'master'
    )
    #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 ($null -ne $LocalCountryBranch) {
            $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 $TargetCommit
        git push -f
    }
}

Export-ModuleMember -Function Rebase-CountryBranches