-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-delete-merged-branches
executable file
·87 lines (79 loc) · 1.78 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
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
# Delete merged branches
local="no"
remote="no"
help="yes"
mergedinto="master"
dryrun="no"
while (( "$#" )); do
if [ "$1" == "-l" ]; then
local="yes"
help="no"
shift
elif [ "$1" == "-r" ] && [ x"$2" != x ]; then
remote="$2"
help="no"
shift 2
elif [ "$1" == "-c" ] && [ x"$2" != x ]; then
mergedinto="$2"
shift 2
elif [ "$1" == "-d" ]; then
dryrun="yes"
shift
else
help="yes"
break;
fi
done
if [ $help == "yes" ]; then
echo
echo "Delete merged branches"
echo
echo "Syntax: git-delete-merged-branches [-l] [-r remote] [-c commit]"
echo
echo "-c <commit> Delete all branches reachable from <commit>. Default is master."
echo "-l Delete local branches."
echo "-r <remote> Delete and push remote branches of <remote>."
echo "-d Dry run. Just print branches that would had been deleted."
echo
fi
if [ $local == "yes" ]; then
for x in `git branch --merged $mergedinto | grep -v '^[*+]'`
do
case "$x" in
master | maint | maint-[0-9][0-9])
echo "Skipping $x"
;;
*)
if [ $dryrun == "yes" ]; then
echo "DRYRUN: git branch -D $x"
else
git branch -D $x
fi
;;
esac
done
fi
if [ $remote != "no" ]; then
for x in `git branch -r --merged $mergedinto --format="%(refname)" | grep "^refs/remotes/$remote/"`
do
branch=${x#refs/remotes/$remote/}
case $branch in
HEAD | master | maint | maint-[0-9][0-9])
echo "Skipping $x"
;;
*)
if [ $dryrun == "yes" ]; then
echo "DRYRUN: git push $remote :$branch"
else
echo "Deleting remote branch $x"
if ! git push $remote :$branch; then
# Branch did probably not exist at $remote
# Delete local remote-tracking branch
git branch -rD $x
fi
fi
;;
esac
done
fi