-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdelete
More file actions
21 lines (14 loc) · 754 Bytes
/
delete
File metadata and controls
21 lines (14 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# Delete all local branches that are merged / deleted branches in remote
# Fetch and prune remote branches
git fetch --prune
# Get the default branch name (either main or master)
default_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | cut -d '/' -f 2)
# If default_branch is not found, default to main
if [ -z "$default_branch" ]; then
default_branch="main"
fi
# Delete local branches that are merged into the default branch
git branch --merged origin/"$default_branch" | grep -v "^\*" | grep -v "^$default_branch$" | xargs -r git branch -d
# Delete local branches that track deleted remote branches
git branch -vv | grep ': gone]' | awk '{print $1}' | grep -v "^$default_branch$" | xargs -r git branch -D