Skip to content

Instantly share code, notes, and snippets.

@derhasi
Last active April 23, 2021 08:46
Show Gist options
  • Save derhasi/e0fb42238bb0dabff646614789f3138f to your computer and use it in GitHub Desktop.
Save derhasi/e0fb42238bb0dabff646614789f3138f to your computer and use it in GitHub Desktop.
Cleans up git repo by rebasing current branches (if possible) and deleting already merged branches
#!/bin/bash
# Cleans up git repo by rebasing local branches (if possible) and deleting already merged branches
set -e
git fetch --all
BASE_BRANCH="origin/master"
CHECKOUT_BRANCH="master"
# Make sure all branches are rebased.
for branch in $(git branch --format="%(refname:short)")
do
echo "============ Rebasing $branch onto $BASE_BRANCH ============"
git rebase --quiet $BASE_BRANCH $branch || git rebase --abort
done
git checkout $CHECKOUT_BRANCH
MERGED_BRANCHES=$(git branch --merged $BASE_BRANCH --format="%(refname:short)" | grep -v master | echo "")
for branch in $MERGED_BRANCHES
do
echo "============ Remove already merged branch $branch ============"
git branch -df $branch
done
echo "============ FINISHED ============"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment