Skip to content

Remove all git branches except master

After working on a project for a while, it’s easy to end up with a lot of legacy branches that are not needed anymore. Removing them one by one would be a cumbersome task. Luckily you don’t have to!

git branch | grep -v "master" | xargs git branch -D

Let me explain each part:

  1. git branch — list all branches
  2. grep -v "master" — remove master from the list
  3. xargs git branch -D — execute git branch -D against remaining branches

So helpful, isn’t it? Catch you next time 👋

Comments

  • O
    Onur

    Sorry it is not helpful except for Linux users.

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
  • Pawel Grzybek
    Pawel Grzybek

    It works great for Linux and macOS users. I am sure the version of this helper for Windows requires very little change. Feel free to figure it out and I will update this post.

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
  • S
    Sakri Koskimies

    It works on any OS with git bash though

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
  • N
    NIkhil

    It is really helpfull

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
  • S
    Sushil Adhikari

    Thanks for this contribution, it works like a charm

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
    • Pawel Grzybek
      Pawel Grzybek

      No worries, I am glad it helped you out Sushil.

      👆 you can use Markdown here

      Your comment is awaiting moderation. Thanks!

What'ya think?

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!