Git Tip of the Day – pruning stale remote-tracking branches

In the last month I read “Pro Git” book. Sometimes I stumble upon something really interesting or useful I didn’t know before. I decided to share some tips with you from time to time, since it can help all of us in our day-to-day work. If you have some further tips, don’t be afraid to share.

Today’s menu of git tips says “pruning stale remote-tracking branches”:

You probably know git remote command. It shows information about your remote repositories. Try this one out:

$ git remote show origin

What you might not know is that this command allows you to prune all your local branches that track an already-deleted remote branch.

I often remove remote feature branches after the changes have been merged into master and the branch is not needed anymore, in order to “tidy up”. But if you checked out that branch into your local one and set it as tracking, git pull won’t delete such local branches for you. So how do you know which local branches doesn’t exist on the server anymore? Easily, just run:

$ git remote prune origin --dry-run

and you’ll receive a list of your stale remote-tracking branches. If you run it without –dry-run option, it will remove them for you.

Happy gitting!


Flattr this

14 thoughts on “Git Tip of the Day – pruning stale remote-tracking branches

  1. This works to delete all of the remote/* branches (those shown by git branch -r), but what about the local branch that was based on that remote? I want to delete these obsolete branches but I can’t find any way to do it other than to delete them each individually.

  2. Great tip.
    Now, after we started working with pull requests using GitHub, which let us delete a done-branch after the merge, the prune option is awesome.

Leave a Reply (Markdown syntax supported)