DEV Community

Vineeth.TR
Vineeth.TR

Posted on

1 1 1 1

🧹 How to Delete All Local Git Branches Except the Current One

Working with Git for a while? Your local repository might be cluttered with old branches you no longer need.

counting

If you're tired of cleaning them one by one, here's a one-liner to delete all local Git branches except the one you're currently on. 🧼


✅ The Command

git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

🧠 What It Does

Let's break it down step by step:

🔍 git rev-parse --abbrev-ref HEAD

This gives you the name of the current branch you're on.

main
Enter fullscreen mode Exit fullscreen mode

📋 git branch

Lists all local branches, like so:

  feature/login
  feature/profile
* main
Enter fullscreen mode Exit fullscreen mode

🔎 grep -v "$(git rev-parse --abbrev-ref HEAD)"

This removes (filters out) the current branch from the list. -v means "exclude matching lines".

🧹 xargs git branch -D

This force-deletes (-D) each branch passed in from the previous output.


⚠️ Warning

This command will force delete all local branches except your current one. That means:

  • Any unmerged changes in those branches will be lost.
  • This does not affect remote branches.
  • Use with caution.

If you want to be safe, replace -D with -d:

git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

This will only delete branches that have been merged into the current branch.


📦 Pro Tip: Make it an Alias

If you find yourself using this often, add it to your shell aliases:

alias git-clean-branches='git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -D'
Enter fullscreen mode Exit fullscreen mode

Then just run:

git-clean-branches
Enter fullscreen mode Exit fullscreen mode

🚀 Summary

When you're done with old branches and want to keep your workspace clean, this one-liner saves you from deleting each branch manually.

No more:

git branch -d branch1
git branch -d branch2
Enter fullscreen mode Exit fullscreen mode

Just one command and your local branches are tidy again. 👌

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay