2# This script cleans up local Git branches which have been merged into
3# the main branch. I use it to clean up the branch view in GitUp
4# (my Git GUI of choice), so I'm not distracted by lots of old branches.
6# It's based on the commands from this Stack Overflow post:
7# https://stackoverflow.com/a/6127884/1558022
12GIT_ROOT=$(git rev-parse --absolute-git-dir)
14if [[ -f "$GIT_ROOT/refs/remotes/origin/HEAD" ]]
16 PRIMARY_BRANCH=$(cat "$GIT_ROOT/refs/remotes/origin/HEAD" \
19elif [[ -f "$GIT_ROOT/refs/remotes/origin/live" ]]
26CURRENT_BRANCH=$(git branch --show-current)
28if [[ "$PRIMARY_BRANCH" = "$CURRENT_BRANCH" ]]
30 print_info "-> Current branch is $CURRENT_BRANCH, which is primary"
32 print_info "-> Primary branch is $PRIMARY_BRANCH"
33 print_info "-> Current branch is $CURRENT_BRANCH"
36for branch in $(git branch --merged "$PRIMARY_BRANCH" | grep -v '*')
38 if [[ "$branch" == "$PRIMARY_BRANCH" ]]
43 if [[ "$branch" == "$CURRENT_BRANCH" ]]
48 git branch --delete "$branch"