-
-
Save Emuentes/80c96c3927911dae6e19 to your computer and use it in GitHub Desktop.
####################################### | |
# 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 |
:D sounds good man!
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.
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.
@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
@chrisng,
Actually, scratch what I said above RE: the delete script.
I tried to delete a remote branch without using push and it didn't work :-(
Furthermore, it has to be "push origin --delete" not just "push --delete" or there is an error saying that --delete can't be done for branch "origin/branch-name" because --delete doesn't work without a given ref.
For that reason I have to keep the cut command in there.