DevOps / Scripts bash Beginner

Git Branch Cleanup

Delete every local branch already merged into main, with a dry-run mode.

bash
#!/bin/bash
git fetch --prune
merged=$(git branch --merged main | grep -v main)
[ -z "$merged" ] && echo Nothing to clean. && exit 0
echo "$merged" | xargs git branch -d

Prerequisites

  • git >= 2.x

Pro Tips

  • → Run with echo first to preview.
  • → Add to post-merge hook.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
bash
Level
Beginner
Read time
2 min
Published
Mar 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.