Created
January 21, 2026 02:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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