-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathremove_branches.bash
66 lines (59 loc) · 1.2 KB
/
remove_branches.bash
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
#!/bin/bash
#############
# Colours #
#############
GREEN=$'\e[0;32m'
RED=$'\e[0;31m'
BLUE=$'\033[0;34m'
NC=$'\e[0m'
##################################
# Help function #
##################################
Help() {
echo $GREEN
echo "Script to remove all branches except the one passed as argument."
echo
echo "Syntax: ./remove_branches [-d|b|h|V]"
echo
echo "options:"
echo "-d Path to the repo directory (required)."
echo "-b main/master branch (required)."
echo "-h Prints help."
echo $NC
}
if [ $# -eq 0 ]; then
Help
exit
fi
# Get the options
while getopts ":h:d:b:" option; do
case $option in
h)
Help
exit
;;
d)
path=${OPTARG}
;;
b)
b=${OPTARG}
;;
?)
echo "Error: Invalid option"
exit
;;
esac
done
if [ -z "$path" ] || [ -z "$b" ]; then
echo "$RED The repository path and the main/master branch are required.$NC"
exit
fi
cd $path
branches_to_remove=($(git branch | cut -c 3- | grep -v $b))
for b in "${branches_to_remove[@]}"; do
echo $BLUE
echo "[i] Removing $b ..."
echo $GREEN
git branch -D $b
echo $NC
done