-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_git_branches.sh
49 lines (40 loc) · 1.17 KB
/
cleanup_git_branches.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
#!/bin/zsh
candidate_branches_to_delete=()
propose_git_branch_cleanup() {
local_branches=($(git for-each-ref --format='%(refname:short)' refs/heads/))
for local_branch in "${local_branches[@]}"; do
if [[ $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/) == *"$local_branch"* ]]; then
echo "Keeping $local_branch"
else
candidate_branches_to_delete+=("$local_branch")
fi
done
echo ""
echo "Proposing to delete branches:"
for b in "${candidate_branches_to_delete[@]}"; do
echo $b
done
}
confirmed_git_branch_cleanup() {
for b in "${candidate_branches_to_delete[@]}"; do
git branch -D $b
done
}
# Get the current branch name
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null)
# Check if the current branch is 'main'
if [[ "$current_branch" == "main" ]]; then
propose_git_branch_cleanup
else
echo "Current branch is: $current_branch"
echo "Must be on main branch to clean up local branches"
exit 1
fi
read -r "REPLY?Do you want to proceed with deleting these local branches? [Y/n] "
echo ""
if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
confirmed_git_branch_cleanup
else
echo "Aborting"
exit 0
fi