Skip to content

Instantly share code, notes, and snippets.

@Sanne
Created July 11, 2012 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sanne/3090687 to your computer and use it in GitHub Desktop.
Save Sanne/3090687 to your computer and use it in GitHub Desktop.
git-deep-prune
#!/bin/bash
# Script to loop on local and remote branches, to delete all those which are
# already merged in master.
# Assumes "origin" and "master" are your references: replace all occurrences
# of "origin" with the name of your personal remote.
#
# Careful with branches which should are meant as tags in the past!
#
# Inspired from http://devblog.springest.com/a-script-to-remove-old-git-branches
# Released under the WTFPL license version 2 http://sam.zoy.org/wtfpl/
# Copyright (c) 2012 Sanne Grinovero
# Prune stale references to remote, and fetch information on new branches:
git fetch --all --prune
# Delete local branches which are merged in master
git branch --merged master | grep -v 'master$' | xargs git branch -D
# Remove remote fully merged branches
git branch -r --merged master | grep "origin/" | grep -v "origin/master" | sed -e 's/\s*origin\///' | xargs -I% git push origin :%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment