Skip to content

Instantly share code, notes, and snippets.

@ErebusBat
Last active November 12, 2015 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErebusBat/23656e11b329c8f3b5f2 to your computer and use it in GitHub Desktop.
Save ErebusBat/23656e11b329c8f3b5f2 to your computer and use it in GitHub Desktop.
Shell script to remove remote branches that have been merged upstream

Git Remove Merged Branches

Use this shell function to remove local git branches that have been merged in your origin repo (say from a GH PR).

Example

$ git_rm_merged_branches
The following branches have been merged and can be removed:
  circleci-tests
  curated-comments
  del-resque-counter
  email-docs
  fix-kp-vids
  fix-suggestion-box
  group-email-dont-die
  hotfix-cached-counts
  prevent-cc-dbl-charges
  reschedule-delete
  rm-packagecloud
  unicorn-simple-config
Press ENTER to purge branches.

Deleted branch circleci-tests (was d8c577d).
Deleted branch curated-comments (was 5f47a88).
Deleted branch del-resque-counter (was 935def7).
Deleted branch email-docs (was 3877806).
Deleted branch fix-kp-vids (was e96b2c9).
Deleted branch fix-suggestion-box (was cf7af66).
Deleted branch group-email-dont-die (was 4b17060).
Deleted branch hotfix-cached-counts (was 324c572).
Deleted branch prevent-cc-dbl-charges (was 4aea326).
Deleted branch reschedule-delete (was 8668973).
Deleted branch rm-packagecloud (was 5e31c59).
alias grmmb=git_rm_merged_branches
function git_rm_merged_branches() {
local branches=$(git branch --merged | grep -Ev '(\*|master)')
echo "The following branches have been merged and can be removed:"
echo $branches
echo "Press ENTER to purge branches."
read continue_signal
printf %s "$branches" | {
while read -r br; do
# We don't use -D here as a failsafe
git branch -d "$br"
done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment