Functions/Sync-GitRepos.ps1


function Sync-GitRepos {
    [CmdletBinding()]
    param (
        [Parameter()] [switch] $Pull,
        [Parameter()] [switch] $CommitAndPush,
        [Parameter()] [string] $GitFolder = "$($env:USERPROFILE)\Git"
    )

    "---------------------------------"
    Get-ChildItem $GitFolder -Directory | ForEach-Object {
        $GitConfigPath = Join-Path $_.FullName ".git"
        if (Test-Path $GitConfigPath) {
            Set-Location $_.FullName
            Write-Host $_.Name -ForegroundColor Green
            "`n"
            $Branches = git branch -a
            if ($Branches -match "main") {
                git switch main
            } elseif ($Branches -match "master") {
                git switch master
            } else {
                Write-Warning "Master or main branch not found, leaving branch unchanged"
            }

            git fetch --all
            git status
            git branch

            if ($Pull) {
                git pull
            }
            if ($CommitAndPush) {
                git add .
                git commit -a -m 'Commit All'
                git push
            }
            "---------------------------------------------------------------`n"
        }
    }
    Set-Location $GitFolder


}