Skip to main content
whitep4nth3r logo

How to delete all merged git branches with one terminal command

Automate your git cleanup! Here's a shell function to add to your bashrc/zshrc file to delete all merged git branches in one command.

⚠️ This post is over two years old and may contain some outdated technical information. Please proceed with caution!

Are you feeling overwhelmed with too many git branches? Do you know which branches have been merged and which can be safely deleted? Do you leave your git branches to pile up on your local machine until your git branch command spits out one hundred terminal pages of text? There's no need to live like this!

A terminal window running the command git branch, showing a list of seven branches.

Here's how you can safely delete all merged git branches with one terminal command, using a little shell function.

❓ Want to delete all squash-merged branches?

Here's a blog post with an updated script that deletes all merged and squashed branches. You'll also learn some more fun things about Git!

The script

To delete all merged git branches on your local machine:

  • add the following code to your .bashrc or .zshrc file (switch out main for what you usually call your main branches if necessary)

  • save the file

  • source the new changes (or reload your terminal)

  • run dam in a git project directory (dam is my acronym for delete all merged)

  • and it's done!

# delete all branches merged into main
dam() {
echo "=== Deleting all merged branches ==="
git checkout main && git branch --merged | grep -vE "(^\*|main)" | xargs git branch -d
echo "☑️ Done!"
}

In short, the code above checks out the main branch, lists all merged branches, searches for and lists branches that are not named exactly "main", and uses git to delete those branches on your local machine.

Here's a detailed breakdown.

git checkout main && git branch --merged

This command checks out the main branch, and lists all branches that can be detected as being merged into the main branch. The official git documentation writes this as "list branches whose tips are reachable from the specified commit (HEAD if not specified)". We're not specifying a commit, so the command will use the HEAD — the current local state of the main branch.

| grep -vE "(^\*|main)"

We use grep (or extended grep using the E flag) to search using extended regular expressions. The -v flag instructs grep to print lines that do not match a given regular expression. The regular expression we're passing in — "(^\*|main)" — matches the characters "main" from the start of the line. So with the -v flag, we're asking grep to find every branch that isn't named "main".

The | (or pipe) at the beginning of the line means we're "piping" — or sending — the output of the previous command (git branch --merged) into grep to perform the regular expression search. Read more about grep.

| xargs git branch -d

Finally, we pipe the result of the egrep search to the xargs command (short for extended arguments). xargs takes input from standard input (basically, a string of text — or a list of git branches in this case), and splits the string where there are spaces into separate arguments that can be used in a command.

For each of the branches returned by egrep above, the script will run git branch -d, which will delete the branch on your local machine.

And here's the script in action in the terminal. Enjoy!

A terminal window showing the command dam running. It shows output that results in the branch refactor-youtube-call being deleted.

Like weird newsletters?

Join 497+ subscribers in the Weird Wide Web Hole to find no answers to questions you didn't know you had.

Subscribe

I stand on stage at what the stack as if it is 1995, which is indicated by the vintage 90s shirt I am wearing. I am holding the mic. I am looking into the distance. I am gesticulating. I am powerful. I command the stage. I am one with the presentation.

Salma Alam-Naylor

I'm a software engineer and developer educator, and I make stuff on the internet. I help developers build cool stuff by writing blog posts, making videos, coding live on the internet, and publishing open source projects. Head of Developer Education at nordcraft.com.

Did this post help you?

☕️ Buy me a coffee

(thank you!)

Related posts

6 Feb 2022

Debug your CSS layouts with this one simple trick

Are you battling with layouts in CSS? Use this one line of CSS to help you debug what's up and get you back on the road to success.

CSS 2 min read →

4 Sep 2025

How to delete all squash-merged local git branches with one terminal command

I wrote a new bash script. And you probably shouldn't use it.

Git 5 min read →