DEV Community

Discussion on: Remove all your local git branches but keep main

Collapse
 
chiangs profile image
Stephen Chiang • Edited

You can create an alias function like this:

removeLocals = "!f() { git branch | grep -v "main" | xargs git branch -D; }; f"
Enter fullscreen mode Exit fullscreen mode

used: git removeLocals

or if you want to account for master vs main

removeLocals = "!f() { git branch | grep -v /"$1/" | xargs git branch -D; }; f"
Enter fullscreen mode Exit fullscreen mode

used: git removeLocals main

I recommend not doing the second one as you may accidentally remove your local version of main/master due to a typo

You could also just make two aliases removeLocalsMain and removeLocalsMaster