Skip to content

Instantly share code, notes, and snippets.

@GrayedFox
Last active October 5, 2022 01:33
Show Gist options
  • Save GrayedFox/2c2c770b6b4f44317b275dd4a69f333f to your computer and use it in GitHub Desktop.
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)
#!/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)
@GrayedFox
Copy link
Author

@GrayedFox
Copy link
Author

GrayedFox commented Jul 26, 2018

Updated to use echo and pipe into xargs instead of doing git branch --merged and piping into grep again (was repeating myself)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment