-
Notifications
You must be signed in to change notification settings - Fork 0
/
.remove_merged_branches
47 lines (38 loc) · 1.52 KB
/
.remove_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
#!/bin/bash
function rmb () {
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "main" ]; then
echo "WARNING: You are on branch $current_branch, NOT main."
fi
echo "Fetching merged branches..."
git remote prune origin
remote_branches=$(git branch -r --merged | grep -v '/main$' | grep -v "/$current_branch$")
local_branches=$(git branch --merged | grep -v 'main$' | grep -v "$current_branch$")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
read -rp "Remove all remote branches? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
# Remove remote branches
git push origin "$(git branch -r --merged | grep -v '/main$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n')"
else
echo "No remote branches removed."
fi
read -rp "Remove all local branches? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
# Remove local branches
git branch -d $(git branch --merged | grep -v 'main$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n' | sed 's/^ //g')
else
echo "No local branches removed."
fi
fi
}