Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created September 14, 2023 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/aecfa7f35de91129775d70ba9bbb5c7f to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/aecfa7f35de91129775d70ba9bbb5c7f to your computer and use it in GitHub Desktop.
Git - PowerShell Script to Clean up Local Merged Branches
## Checkout the main branch
## If your main branch name is different, use that name instead of 'main'
git checkout main
## This will remove the branches
## which are present on local but are deleted on remote
git fetch --prune
## The below command will get all branches which are merged
## Then it will remove the names 'main', 'master', 'develop' from the list
## Then for each branch name, it will run git branch delete command
git branch --merged `
| Where-Object { $_ -notmatch "^(\\*|main|master|develop)" } `
| ForEach-Object { git branch -d $_.Trim() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment