-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-branch-cleanup-based-on-master-branch.sh
executable file
·55 lines (49 loc) · 1.52 KB
/
git-branch-cleanup-based-on-master-branch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
echo "* Fetching remotes"
git fetch --all
echo "### Pruning remote branches "
# git branch prune origin
local_branches=$(git branch --merged origin/master | cut -c 2-| grep -v 'master$')
echo "### Cleaning local,merged branches '$(echo $local_branches|xargs)'"
echo ""
echo ""
for local_branch in $local_branches; do
echo "git branch -D ${local_branch/\//}"
done
echo ""
echo ""
remote_branches=$(git branch -r --merged origin/master | grep -Ev '/(master|HEAD)$')
echo "### Cleaning remote,merged branch references '$(echo $remote_branches|xargs)'"
echo ""
echo ""
echo $remote_branches | xargs -I{} echo "git branch -d -r {}";
merged_branches=$(git for-each-ref refs/heads/ '--format=%(refname:short)' |
while read branch; do
mergeBase=$(git merge-base origin/master $branch) &&
[[ $(git cherry origin/master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] &&
echo $branch;
done
)
echo ""
echo ""
echo "### Cleaning squashed,merged branches '$(echo $merged_branches|xargs)'"
echo ""
echo ""
for merged_branch in $merged_branches; do
echo "git branch -D ${merged_branch/\// /}"
done
echo ""
echo ""
echo "### Deleting remote branches '$(echo $remote_branches|xargs)'"
echo ""
echo ""
for b in $remote_branches; do
# `${b/\// /}` splits `origin/name` into `origin name`
# TODO: push can accept multiple names per origin
echo "git push --delete ${b/origin\//origin }"
echo "https://github.com/Wonolo/platform-core/tree/${b/origin\//}"
echo ""
done
echo ""
echo ""
# vim: ft=sh