Skip to content

Instantly share code, notes, and snippets.

@rud
Created October 21, 2013 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rud/7084313 to your computer and use it in GitHub Desktop.
Save rud/7084313 to your computer and use it in GitHub Desktop.
Simple script for deleting local already merged branches, and prune remote tracking branches where the report part no longer exists. As a practical convenience, it will not delete a few long-lived local branches such as "develop", "master", and "production". This is built on ideas from http://stevenharman.net/git-clean-delete-already-merged-bran…
#!/bin/sh
# Simple script for pruning local branches already merged,
# and remove remote tracking branches that have been deleted
# Default to "origin" if no remote is passed
remote=${1-origin}
# Branches to keep, regardless of their current merge state
permanent_branches="develop|master|production"
# Remove local already merged branches
git branch --merged | grep -v -E "(\*|$permanent_branches)" | xargs -n 1 git branch -d
# Retry with whatever 'git remote' outputs
git remote prune $remote || git remote prune `git remote`
Copy link

ghost commented Jun 20, 2017

Suggested addition to ~/.gitconfig:

[aliases]
  cleanup-merged = !prune-git

This assumes the above is available as prune-git in $PATH. Then it can be invoked with git cleanup-merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment