One way to remove local merged tracking branches

My git workflow involves creating a lot of git feature branches, as remote tracking branches on origin. They eventually get merged and deleted (via github PR), but i still have dozens of them lying around.

Via googling, getting StackOverflow answers, and sort of mushing some stuff I don’t totally understand together, here’s one way to deal with it, create an alias git-prune-tracking.  In your ~/.bash_profile:

alias git-prune-tracking='git branch --merged | grep -v "*" | grep -v "master" | xargs git branch -d; git remote prune origin'

And periodically run git-prune-tracking from a git project dir.

I do not completely understand what this is doing I must admit, and there might be a better way? But it seems to work. Anyone have a better way that they understand what it’s doing?  I’m kinda surprised this isn’t built into the git client somehow.

Advertisements

3 thoughts on “One way to remove local merged tracking branches”

  1. I think `git fetch –prune` might apply?

    > Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not
    subject to pruning if they are fetched only because of the default tag auto-following or due to a –tags
    option. However, if tags are fetched due to an explicit refspec (either on the command line or in the
    remote configuration, for example if the remote was cloned with the –mirror option), then they are also
    subject to pruning.

  2. Bill, I think that just gets rid of the `origin` branches, the same thing that my version’s “git remote prune origin” does. This is actually _different_ from your remote tracking branches. I may not have the terminology right, but there are definitely two things here. Git is confusing.

    Pretty sure Andy’s `git gc` also does something entirely different, and is run automatically now and then by git anyway, and doesn’t get rid of remote tracking branches that no longer have branches on the remote.

Leave a Reply

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