Delete local branch with regex

Sometimes i’m too lazy to do most of the things manually. Copy and paste ? that’s annoying.

Lately i had a bunch of local git branches. I never deleted even after i push. I’m not sure that too much local branches will harm to git environment or not. But it doesn’t looks good whenever you try git branch and saw the long list of branches.

I just think it’s time to clean.

the command is easy

    git branch -D [yourlocalbranch]

The thing is that you have to do 100th times if you have 100 local branches.

    winn:shophk winn$ git branch
      SHOP-6155
      SHOP-8134
      SHOP-8316
      SHOP-9214
      SHOP-9260
      SHOP-9314
      test_sharedstock
      testsolr
    * trunk
      ultron_qa
      ultron_tw_dts

I just googling around and just found that i can do with regex.

The syntax is easy in my example

git branch -D `git branch | grep -E 'SHOP-*'`

There you go…

winn:shophk winn$ git branch -D `git branch | grep -E 'SHOP-*'`
    Deleted branch SHOP-6155 (was 8fad0ce).
    Deleted branch SHOP-8134 (was 99b1d59).
    Deleted branch SHOP-8316 (was bb475c7).
    Deleted branch SHOP-9214 (was dc1438b).
    Deleted branch SHOP-9260 (was b442948).
    Deleted branch SHOP-9314 (was 0dacf69).

Happy Coding! ^_^