Last active
October 5, 2022 01:33
-
-
Save GrayedFox/2c2c770b6b4f44317b275dd4a69f333f to your computer and use it in GitHub Desktop.
Delete local branches that have been merged into master (except master itself, with optional -f)
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 bash | |
BRANCH="main" | |
if [ $# -eq 1 ] ; then | |
BRANCH=$1 | |
fi | |
MERGED_BRANCHES=$(git branch --merged $BRANCH | grep -v '^[ *]* '"$BRANCH"'$') | |
if [ -z "$MERGED_BRANCHES" ]; then | |
exit 0 | |
fi | |
echo "$MERGED_BRANCHES" | xargs git branch -d | |
# git trim :: list all branches that are merged into a specified branch (default: main), | |
# filtering out the target branch itself, and then delete only those branches | |
# that are fully merged (safe, deletes local branches only) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use echo and pipe into xargs instead of doing
git branch --merged
and piping intogrep
again (was repeating myself)