DEV Community

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

Collapse
 
amirhe profile image
Amir.H Ebrahimi

make an alias out of it which also protect master and dev branches,
and also try to delete them in safer way with -d instead of -D.

alias gitrm='git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
darthwalsh profile image
Carl Walsh

git branch --merged doesn't work well if your team uses GitHub squash commits to merge PRs: stackoverflow.com/a/19309568/771768

I've had a lot of success with my script that:

  1. Get all local git branches
  2. Uses github API to query for all merged/closed PRs with the same branch names
  3. Deletes the remote git branch on GitHub (useful if your repo doesn't do this automatically)
  4. Deletes the local git branch (but if that branch is currently checked out, switch to default branch first)