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!
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.
Thanks for the tip! 🙂
very nice, exactly what iam searching for
You can also try “git fetch -p” or “git fetch –prune”
This removes remote-tracking branches not existing on the remote, but not non-tracking it seems.
helpful tip! thanks
Thanks really helpful
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.
Thank you… very helpful!
Best explanation yet – thanks Kparal!
Really of help…
We use git-obsolete-branch-remover useful as it lists or removes local or remote Git branches based on last commit date and merge status.
https://github.com/vackosar/git-obsolete-branch-remover
Automated removal of stale branches that are already merged in master
https://github.com/nikcomestotalk/removegitstalebranches