Created
May 1, 2017 20:19
-
-
Save WintersMichael/21db70eb2c5a31144a522a711818877b to your computer and use it in GitHub Desktop.
Pull master and clean dead local branches after a PR
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 | |
echo "Switching to master and pulling ..." | |
git checkout master | |
if [[ ! $? -eq 0 ]]; then | |
echo | |
echo Unable to checkout master, aborting. | |
exit | |
fi | |
git pull | |
echo Pruning dead remote branch pointers ... | |
git fetch --prune | |
# get list of remote branch names, find all local branch names which aren't in the list of remote names (inverse grep) | |
#TODO: I haven't tested this for whole-word matching, eg branch 'fixes' should be deleted but not 'fixes-dev' | |
DEAD_BRANCHES=$(git branch --remotes | awk '{print $1}' | egrep --invert-match --file=/dev/fd/0 <(git branch -vv) | awk '{print $1}') | |
#convert newline string to bash array | |
IFS=$'\n' read -r -a DEAD_BRANCHES_ARRAY <<< "$DEAD_BRANCHES" | |
DEAD_BRANCH_COUNT=${#DEAD_BRANCHES_ARRAY[@]} | |
if [[ "$DEAD_BRANCH_COUNT" -lt 1 ]]; then | |
echo | |
echo "No branches to clean, exiting" | |
exit | |
fi | |
echo | |
echo "Found $DEAD_BRANCH_COUNT local branches to delete:" | |
for branch in ${DEAD_BRANCHES_ARRAY[@]}; do | |
echo " $branch" | |
done | |
echo | |
read -p "Are you sure? [y/n] " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
for branch in ${DEAD_BRANCHES_ARRAY[@]}; do | |
git branch -d $branch | |
done | |
else | |
echo Aborting | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment