Skip to content

Instantly share code, notes, and snippets.

@htulipe
Forked from nikreiman/gist:5458386
Last active July 11, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save htulipe/ba250e1c9231fad05e2a to your computer and use it in GitHub Desktop.
Save htulipe/ba250e1c9231fad05e2a to your computer and use it in GitHub Desktop.
Clean up local branches that are already on master (credit to original gist)
function git-branch-current() {
printf "%s\n" $(git branch 2> /dev/null | grep -e ^* | tr -d "\* ")
}
function git-branch-cleanup() {
local currentBranch=$(git-branch-current)
local otherBranch=
for otherBranch in $(git branch | grep -v $currentBranch) ; do
printf "Branch %s:\n" "$otherBranch"
printf " HEAD commit is: %s\n" "$(git log --oneline -n 1 $otherBranch)"
local gerritChangeId=$(git log --format=full -n 1 $otherBranch | tail -1 | cut -d ':' -f 2)
if ! [ "$gerritChangeId" ] ; then
printf " HEAD of %s does not have a Gerrit Change-Id\n" "$otherBranch"
continue
fi
local mergedCommit=$(git log --oneline -n 1 --grep="$gerritChangeId" $currentBranch)
if [ "$mergedCommit" ] ; then
printf " Commit merged to %s: %s\n" "$currentBranch" "$mergedCommit"
local reply
read -p " Delete branch $otherBranch [y/N]? " reply
reply=${reply:-n}
if [ $reply = "y" ] ; then
git branch -D $otherBranch
fi
else
echo " Branch unmerged, moving on"
fi
done
}
git-branch-cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment