Tips & Tricks Tips & Tricks How to clean up already merged git branches?

If you're using git, you probably have a lot of branches. If not, you probably should. Feature branches, long running branches, release branches, hotfix branches...

Yet, the tree can become quite big, and you will want to clean things up a bit from time to time.

Short answer

Install git-sweep:

$ pip install git-sweep || easy_install git-sweep

Preview branches already in upstream:

$ git sweep preview

Fetching from the remote
These branches have been merged into master:

  dev-django
  dev-django-migration
  dev-okdocker
  dev-wip-release-and-machines

To delete them, run again with `git-sweep cleanup`

If you're ok with the changes, delete the branches:

$ git sweep cleanup

You're done, you just need to ask your teamates to cleanup their local copies too for outdated references, using:

$ git fetch --prune

Details

As git repositories are a commit tree, git sweep will find all commits that are ancestors of a the main branch and suggest them for deletion. By default, it considers the master branch to be the reference. If you want to use another branch for this purpose, you can change it (and/or change the upstream):

$ git-sweep preview --master=beta --origin=bitbucket

Eat at Joe's

Git branches are cheap! Embrace branching, have lots of branches, make some more, try things, and be confident in your ease to clean them up when they get out of date.

Not sure what kind of branches you should create or not? Take inspiration from a popular branching flow like git flow (see below).

Share the love!

Liked this article? Please consider sharing it on your favorite network, it really helps me a lot!

You can also add your valuable insights by commenting below.