Skip to content

Instantly share code, notes, and snippets.

@Sephsekla
Created October 17, 2022 09:16
Show Gist options
  • Save Sephsekla/3e2321bde9c9eb9fb814be91ca4d923e to your computer and use it in GitHub Desktop.
Save Sephsekla/3e2321bde9c9eb9fb814be91ca4d923e to your computer and use it in GitHub Desktop.
git cleanup-merged - Deletes all local branches that are fully merged into the remote HEAD branch.
#!/bin/bash -e
# git cleanup-merged
# Deletes all local branches that are fully merged into the remote HEAD branch.
# Useful for a git flow workflow, allows quick deletion of completed feature branches.
CURRENT_BRANCH=$(git branch --show-current)
REMOTE_HEAD=$(git symbolic-ref --short refs/remotes/origin/HEAD)
DEFAULT_BRANCH=${REMOTE_HEAD#"origin/"}
git branch --merged $DEFAULT_BRANCH | grep -E -v "^(\*)? *($DEFAULT_BRANCH|$CURRENT_BRANCH)$" | xargs git branch -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment