DEV Community

Jonathan Irvin
Jonathan Irvin

Posted on

Removing Git Branch Bloat

If you have too many Git branches, bookmark and like this page.

Note: You'll see "(^*|master|main|develop|release)" a lot in the below scripts. This is a regular expression that excludes those long-lived branches so those won't be considered.

To list all local branches that have been merged into the current branch:

git branch --merged | egrep -v "(^*|master|main|develop|release)"
Enter fullscreen mode Exit fullscreen mode

To list all branches that have been merged into the current branch:

git branch -r --merged | egrep -v "(^*|master|main|develop|release)"
Enter fullscreen mode Exit fullscreen mode

To PURGE all local branches that have been merged into the current branch:

git branch --merged | egrep -v "(^*|master|main|develop|release)" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

STOP!

Be very, VERY certain you know what you are doing. This following command modifies the shared repo and unless there is a perfect clone, it has the potential to remove branches that people don’t have a reference to.

To PURGE all REMOTE branches that have been merged into the current branch:

git branch --merged | egrep -v "(^*|master|main|develop|release)" | xargs git push --delete origin
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bias profile image
Tobias Nickel

cool, I give thia a try