Delete Git Branches That Have Been Merged

By Dave Ceddia

Got a lot of old git branches hanging around? Here’s a little script that will delete the branches that have been marged.

It’ll print out the branches to be deleted, and then prompt for whether you want to delete them.

If your top-level branch isn’t called “main”, customize the MAIN variable to match.

Script: Delete old git branches


# Change this to match the name of your top level branch

echo "These branches have been merged into $MAIN and will be deleted:"
git branch --merged $MAIN | grep -v "^\* $MAIN"

read -p "Continue? [y/N] " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
  exit 1

git branch --merged $MAIN | grep -v "^\* $MAIN" | xargs -n 1 -r git branch -d

Copy/paste this into a file in your repo or elsewhere (like and make it executable with chmod +x

Bonus: if you want this file to be ignored, but you don’t want to clutter up the shared .gitignore file with your own local scripts, you can edit .git/info/exclude and list this file there. That file works as a local gitignore.

Learning React can be a struggle — so many libraries and tools!
My advice? Ignore all of them :)
For a step-by-step approach, check out my Pure React workshop.

Pure React plant

Learn to think in React

  • 90+ screencast lessons
  • Full transcripts and closed captions
  • All the code from the lessons
  • Developer interviews
Start learning Pure React now

Dave Ceddia’s Pure React is a work of enormous clarity and depth. Hats off. I'm a React trainer in London and would thoroughly recommend this to all front end devs wanting to upskill or consolidate.

Alan Lavender
Alan Lavender