Last active
March 7, 2024 17:41
-
-
Save raphaelbs/72dd1dca73f03f1bcb16a31e8914a38f to your computer and use it in GitHub Desktop.
Prune git branches and update default branch
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 | |
BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') | |
echo "Update changes" | |
git fetch -u origin $BRANCH:$BRANCH | |
git pull --ff-only origin $BRANCH:$BRANCH | |
echo "Changing branch to $BRANCH" | |
git checkout $BRANCH | |
echo "Prunning local branches that were removed in remote..." | |
git remote update --prune | |
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done | |
echo "Git GCing" | |
git gc | |
git prune | |
echo "Done" | |
echo "Listing remaining branches:" | |
git branch --list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggestion:
I've added a little change to save the branches list in a file and open it to edition, this way I can exclude branches I don't want to remove now before closing the file.