# Git Multi-Repository Sync Script (PowerShell) # For syncing mirror repository to your personal repository param( [switch]$UseRebase = $false, [switch]$DryRun = $false ) function Write-ColorOutput { param( [string]$Message, [string]$Color = "White" ) Write-Host $Message -ForegroundColor $Color } function Check-GitStatus { $status = git status --porcelain if ($status) { Write-ColorOutput "Warning: Working directory is not clean. Please commit or stash your changes first!" "Yellow" git status return $false } return $true } function Sync-Repositories { Write-ColorOutput "Starting repository sync..." "Cyan" # Check working directory status if (-not (Check-GitStatus)) { if (-not $DryRun) { return } } # Fetch latest code from upstream Write-ColorOutput "Fetching latest code from upstream..." "Green" if (-not $DryRun) { git fetch upstream if ($LASTEXITCODE -ne 0) { Write-ColorOutput "Failed to fetch upstream code!" "Red" return } } else { Write-ColorOutput "[DRY RUN] git fetch upstream" "Gray" } # Check if there are new commits $upstreamCommits = git log HEAD..upstream/master --oneline if ($upstreamCommits) { Write-ColorOutput "Found new commits from upstream:" "Yellow" Write-ColorOutput $upstreamCommits "Gray" if ($UseRebase) { Write-ColorOutput "Merging code using rebase..." "Green" if (-not $DryRun) { git rebase upstream/master if ($LASTEXITCODE -ne 0) { Write-ColorOutput "Rebase failed! Please resolve conflicts manually." "Red" return } } else { Write-ColorOutput "[DRY RUN] git rebase upstream/master" "Gray" } } else { Write-ColorOutput "Merging code using merge..." "Green" if (-not $DryRun) { git merge upstream/master if ($LASTEXITCODE -ne 0) { Write-ColorOutput "Merge failed! Please resolve conflicts manually." "Red" return } } else { Write-ColorOutput "[DRY RUN] git merge upstream/master" "Gray" } } # Push to your repository Write-ColorOutput "Pushing to your repository..." "Green" if (-not $DryRun) { if ($UseRebase) { git push origin master --force-with-lease } else { git push origin master } if ($LASTEXITCODE -ne 0) { Write-ColorOutput "Push failed!" "Red" return } } else { $pushCmd = if ($UseRebase) { "git push origin master --force-with-lease" } else { "git push origin master" } Write-ColorOutput "[DRY RUN] $pushCmd" "Gray" } Write-ColorOutput "Sync completed successfully!" "Green" } else { Write-ColorOutput "No new commits from upstream. Checking for local changes..." "Blue" # Check if there are local changes to push $localCommits = git log origin/master..HEAD --oneline if ($localCommits) { Write-ColorOutput "Found local changes to push:" "Yellow" Write-ColorOutput $localCommits "Gray" Write-ColorOutput "Pushing local changes..." "Green" if (-not $DryRun) { git push origin master if ($LASTEXITCODE -ne 0) { Write-ColorOutput "Push failed!" "Red" return } } else { Write-ColorOutput "[DRY RUN] git push origin master" "Gray" } Write-ColorOutput "Push completed!" "Green" } else { Write-ColorOutput "Nothing to sync." "Blue" } } } function Show-Help { Write-ColorOutput "Git Multi-Repository Sync Script" "Cyan" Write-ColorOutput "Usage: .\sync-repos.ps1 [options]" "White" Write-ColorOutput "" Write-ColorOutput "Options:" "Yellow" Write-ColorOutput " -UseRebase Use rebase instead of merge" "White" Write-ColorOutput " -DryRun Preview mode, don't execute actual git commands" "White" Write-ColorOutput " -Help Show this help message" "White" Write-ColorOutput "" Write-ColorOutput "Examples:" "Yellow" Write-ColorOutput " .\sync-repos.ps1 # Sync using merge" "White" Write-ColorOutput " .\sync-repos.ps1 -UseRebase # Sync using rebase" "White" Write-ColorOutput " .\sync-repos.ps1 -DryRun # Preview mode" "White" } # Main program if ($args -contains "-Help" -or $args -contains "--help" -or $args -contains "-h") { Show-Help return } Write-ColorOutput "Git Multi-Repository Sync Tool" "Cyan" Write-ColorOutput "==============================" "Cyan" # Show current configuration Write-ColorOutput "Current remote repository configuration:" "Blue" git remote -v Write-ColorOutput "" if ($DryRun) { Write-ColorOutput "Preview mode - no actual git commands will be executed" "Yellow" } if ($UseRebase) { Write-ColorOutput "Using rebase mode" "Yellow" } else { Write-ColorOutput "Using merge mode" "Yellow" } Write-ColorOutput "" Sync-Repositories