Skip to content

Instantly share code, notes, and snippets.

@michaelsauter
Last active February 16, 2021 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelsauter/297c0a67ba277b9c508f933ef9f70cdf to your computer and use it in GitHub Desktop.
Save michaelsauter/297c0a67ba277b9c508f933ef9f70cdf to your computer and use it in GitHub Desktop.
Find and remove old Git branches
# Update local repo
git fetch origin --prune
# Merged branches at least 2 months old
for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | grep "months" | awk '{print $(NF)}' | sed -e "s/^origin\///" | xargs git push origin --delete --dry-run
# All branches at least a year old
for branch in `git branch -r | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | grep "1 year" | awk '{print $(NF)}' | sed -e "s/^origin\///" | xargs git push origin --delete --dry-run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment