-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitcleanbranches.sh
More file actions
executable file
·29 lines (22 loc) · 915 Bytes
/
gitcleanbranches.sh
File metadata and controls
executable file
·29 lines (22 loc) · 915 Bytes
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
#!/usr/bin/env bash
# main branch (switch to master for old repos)
main="main"
# remotes: the project's repo, and your fork
remoteProject="upstream"
remoteFork="origin"
git fetch $remoteProject --prune
git fetch $remoteFork --prune
# find release branches
branches=$(git branch -r | grep $remoteProject | grep release- | awk '{split($0,a,"/"); print a[2]}' | tr '\n' ' ')
# prepend main
branches="$main $branches"
echo "Branches: $branches\n\n"
for branch in $(echo $branches)
do
echo "Branch: $branch"
echo "Deleting local merged branches"
git branch --merged ${remoteProject}/${branch} | grep -v $branch | xargs -r git branch -d
echo "Deleting remote merged branches"
git branch -r --merged ${remoteProject}/${branch} | grep ${remoteFork}/ | grep -v /$branch | grep -v /HEAD | grep -v /$main | awk '{split($0,a,"/"); print a[2]}' | xargs -r git push $remoteFork --delete
echo
done