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.