Skip to content

Instantly share code, notes, and snippets.

@loudpumpkins
Last active June 1, 2025 01:10
Show Gist options
  • Save loudpumpkins/9c992d6633a9083e871f128727e2b240 to your computer and use it in GitHub Desktop.
Save loudpumpkins/9c992d6633a9083e871f128727e2b240 to your computer and use it in GitHub Desktop.
script-github-sync-branches
#########################
# 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