Skip to content
Theme:

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!
    • A
      Andre

      It works great in windows, you just need to use an advanced console, like ComEmu, for instance. Cheers

      👆 you can use Markdown here

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

        Thanks a lot for clarification Andre.

        👆 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!
  • H
    HaddeeAnn

    Thanks! Works. As a note, seems to also work without the quotes around the "master" (or branch name)

    👆 you can use Markdown here

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

      Nice tip, thanks!

      👆 you can use Markdown here

      Your comment is awaiting moderation. Thanks!
  • D
    Dmitry

    works on latest macOS, thanks for the simple and clear instruction :)

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!