Last active
June 1, 2025 01:10
-
-
Save loudpumpkins/9c992d6633a9083e871f128727e2b240 to your computer and use it in GitHub Desktop.
script-github-sync-branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ######################### | |
| # Sync a specific branch | |
| # checkout the branch to reset/sync to origin repo | |
| $branchName = "alex/branch" | |
| git checkout $branchName | |
| # discard uncommitted changes | |
| git reset --hard | |
| # Reset Local Branch to Match origin | |
| git fetch origin | |
| git reset --hard origin/$branchName | |
| ################################ | |
| # Sync all branches with origin | |
| # Delete local branches that were deleted in origin | |
| git remote prune origin | |
| # Delete local branches that exist only local (never pushed) | |
| $branchesToDelete = git branch -vv | Where-Object { -not $_.Contains('origin/') } | ForEach-Object { $_.Split(' ')[2] } | |
| foreach ($branch in $branchesToDelete) { | |
| if ($branch -ne 'main' -and $branch -ne 'master') { | |
| Write-Host "Deleting branch $branch" | |
| git branch -D $branch | |
| } else { | |
| Write-Host "Skipped branch $branch (default branch)" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment