Skip to content

Instantly share code, notes, and snippets.

@Emuentes
Last active October 12, 2020 19:44
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Emuentes/80c96c3927911dae6e19 to your computer and use it in GitHub Desktop.
Save Emuentes/80c96c3927911dae6e19 to your computer and use it in GitHub Desktop.
SCRIPT-ONE: will print the names of the branches that have been merged into develop AND master in green. Also, branches that are only merged into develop but not master are listed in red. ---- SCRIPT-TWO: will delete the fully merged branches, those listed in green when you run SCRIPT-ONE
#######################################
# SCRIPT 1 - PREVIEW BRANCH DELETIONS #
#######################################
# will print the names of the branches that have been merged into develop and master in green and branches that are only merged into develop but not master in red.
BRANCHES_IN_MASTER=`git branch -r --merged origin/master | grep -v -E '(\*|master$|develop$)' | cut -f2- -d '/' | tr '\n' ';'` && export BRANCHES_IN_MASTER && git branch -r --merged origin/develop | grep -v -E '(\*|master$|develop$)' | cut -f2- -d '/' | xargs -L1 bash -c 'if [ $(grep -o "$0" <<< "$BRANCHES_IN_MASTER" | wc -l) -gt 0 ] ; then printf "\e[0;32m $0 \e[0m\n"; else printf "\e[0;31m $0 is merged into DEVELOP but not MASTER \e[0m\n"; fi';
#################################################################################################################################
# SCRIPT 2 - DANGER -- RUN AT YOUR OWN RISK -- The following script will DELETE the branches listed in the above preview script #
#################################################################################################################################
# The following script is the same as the above script
BRANCHES_IN_MASTER=`git branch -r --merged origin/master | grep -v -E '(\*|master$|develop$)' | cut -f2- -d '/' | tr '\n' ';'` && export BRANCHES_IN_MASTER && git branch -r --merged origin/develop | grep -v -E '(\*|master$|develop$)' | cut -f2- -d '/' | xargs -L1 bash -c 'if [ $(grep -o "$0" <<< "$BRANCHES_IN_MASTER" | wc -l) -gt 0 ] ; then printf "\e[0;32m DELETING: $0 \e[0m\n" && git push origin --delete $0; else printf "\e[0;31m $0 is merged into DEVELOP but not MASTER \e[0m\n"; fi';
git remote update -p
@micros123
Copy link

Great script, very useful and safe. Only thing I noticed is that Bitbucket starts resetting the connection after ±20 requests. But you rerun the script after a half a minute for a couple of times, you can get through them all.

@Emuentes
Copy link
Author

Glad you found the script useful @micros123. I had been using Gitlab when I originally needed this script. I'm using Bitbucket now. I wonder if there is something I can do about the timeout. I'll look into it. Thanks.

@Emuentes
Copy link
Author

@micros123, not sure if you manage the bitbucket server yourself or not but if you have the access or know the team who would be able to change the setting. I think this is the documentation on how to address that:
https://confluence.atlassian.com/bitbucketserverkb/git-ssh-push-timeout-on-large-changes-779171775.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment