Git Tips #5 - Prune branches to keep your repro in good shape

When you've been working with Git for a while, you'll soon find you have lots of out of date and stale branches in your repro. Today we'll look at fixing that.

Prune stale remote tracking branches

Every time you fetch from a remote, tracking braches are pulled down from the remote. After a while you'll find you have lots of origin/* branches in your local repro which no longer exist on the remote. To find out which branches they are, run this command:

git remote prune origin --dry-run

To go ahead and delete those branches it's:

git remote prune origin

You can also prune when you fetch from a remote. To do this use the --prune option:

git fetch --prune

Prune local branches which have been merged

Now that the remote tracking branches are in good shape, let's look at local branches which have been merged. To get a list, run this command:

git branch --merged

And to delete a branch:

git branch -d branch-to-delete

Prune local branches which have not been merged

And finally, let's delete stale branches which have been abandoned or no longer needed. To get a list, run this command:

git branch --no-merged

And to delete a branch:

git branch -D un-merged-branch-to-delete

References