Git Clean-Up Commands

Sometimes you may find a git repository with a lot of branches and not all of them necessary.

Because of that, you may want to do some cleanup, so I’ve prepared a list of very useful commands:

  • Remove Merged Branches

just to see them / or number them

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 echo

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | wc -l

and to really delete those branches

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 git push origin --delete 

* these commands take care of the `master branch. If you need to keep another branch, just change the grep -v “^.*master”

  • Remove every remote (beside ‘master)

even much simple + you can use similar commands to see/number these branches

git branch -r | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 git push origin --delete 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.