Skip to content

Instantly share code, notes, and snippets.

@megahertz
Created January 21, 2026 02:19
Show Gist options
  • Select an option

  • Save megahertz/11135eea9f4300ee91e050ccb41aeae3 to your computer and use it in GitHub Desktop.

Select an option

Save megahertz/11135eea9f4300ee91e050ccb41aeae3 to your computer and use it in GitHub Desktop.
Removes any local branches except for main/master and pulls the latest changes from the origin. It's helpful to run once a feature has been merged
#!/usr/bin/env sh
set -e
master_names='main|master'
branch_regexp='s/[^A-Za-z0-9_\/-]*//g'
master_branch="$(git branch | grep -E "${master_names}" | sed "${branch_regexp}" | head -n 1)"
branches="$(git branch | grep -v -E "${master_names}" | sed "${branch_regexp}")"
echo "Master branch: ${master_branch}"
echo 'Are you sure you want to delete these branches:'
echo "${branches}"
read -p "? [y/n]" choice
case "$choice" in
y|Y)
git checkout "${master_branch}"
git pull -p
git branch -D $(echo "${branches}" | xargs)
;;
*)
echo Canceled
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment