Skip to content
April 15, 2011 / Kamil Páral

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

About these ads

9 Comments

Leave a Comment
  1. Jay Miller / Aug 1 2011 23:56

    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. Michal / Aug 4 2011 13:33

    Thanks for the tip! :)

  3. FIsh / May 16 2012 15:06

    very nice, exactly what iam searching for

  4. You can also try “git fetch -p” or “git fetch –prune”

    • Anonymous / Feb 6 2013 14:38

      This removes remote-tracking branches not existing on the remote, but not non-tracking it seems.

  5. Xilinx / Apr 18 2013 14:59

    helpful tip! thanks

  6. peeyush singla / Feb 26 2014 11:32

    Thanks really helpful

  7. eyalgo / Apr 2 2014 09:51

    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.

Trackbacks

  1. 用 git prune 清理掉在 github 已經 deleted 的 local branch | KKWT

Leave a Reply (Markdown syntax supported)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: