Last active
April 23, 2021 08:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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