Skip to content

Commit 4a38e4a

Browse files
committed
feat: add scripts to delete merged branches
1 parent a56001a commit 4a38e4a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

scripts/delete

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
# Delete all local branches that are merged / deleted branches in remote
4+
5+
# Fetch and prune remote branches
6+
git fetch --prune
7+
8+
# Get the default branch name (either main or master)
9+
default_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | cut -d '/' -f 2)
10+
11+
# If default_branch is not found, default to main
12+
if [ -z "$default_branch" ]; then
13+
default_branch="main"
14+
fi
15+
16+
# Delete local branches that are merged into the default branch
17+
git branch --merged origin/"$default_branch" | grep -v "^\*" | grep -v "^$default_branch$" | xargs -r git branch -d
18+
19+
# Delete local branches that track deleted remote branches
20+
git branch -vv | grep ': gone]' | awk '{print $1}' | grep -v "^$default_branch$" | xargs -r git branch -D
21+

0 commit comments

Comments
 (0)