Skip to content

Instantly share code, notes, and snippets.

@johnkors
Last active March 22, 2023 22:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnkors/2c79a11c6372a84bf3237954968594f1 to your computer and use it in GitHub Desktop.
Save johnkors/2c79a11c6372a84bf3237954968594f1 to your computer and use it in GitHub Desktop.
PowerShell script to delete branches merged to master
# update local list of pruned branches on the remote to local:
git fetch --prune
# delete branches on remote origin that have been merge to master
git branch --merged remotes/origin/master -r | %{$_.trim().replace('origin/', '')} | ?{$_ -notmatch 'master'} | %{git push --delete origin $_}
# delete local branches that have been merged to master
git branch --merged remotes/origin/master | %{$_.trim()} | ?{$_ -notmatch 'master'} | %{git branch -d $_}
# remove stale refs (local refs to branches that are gone on the remote)
git remote prune origin
@joost00719
Copy link

Comments in powershell are with the "#" symbol, not with "//".
Other than that, thanks for the script! Works like a charm.

@johnkors
Copy link
Author

johnkors commented Mar 4, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment