-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-delete-merged-branches
executable file
·23 lines (17 loc) · 1.12 KB
/
git-delete-merged-branches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env bash
set -euo pipefail
main=`git-main-branch`
# Delete regular merges
git branch --merged $main | cut -b 3- | (grep --invert-match "^$main\$" || echo) | xargs git branch -d
# Two strategies for deleting merged branches:
# Delete branches whose diff from merge-base matches a commit on main branch
# i.e. branches merged in a single squashed commit
# based on https://github.com/not-an-aardvark/git-delete-squashed
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do (mergeBase=$(git merge-base "$main" "$branch") && [[ $(git cherry "$main" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]] && git branch -D "$branch") || true; done
# Delete squashed merges by figuring out branches that would apply no diff to
# main if merged. This attempts to address branches merged in multiple
# squashed commits.
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
[[ "$branch" == "$main" ]] && continue
([ -z "$(git merge-tree "$(git merge-base "$main" "$branch")" "$main" "$branch")" ] && git branch -D "$branch") || true
done