Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@chrisrng

This comment has been minimized.

Copy link

@chrisrng chrisrng commented Feb 16, 2016

Here is also a way to delete branches that have been merged (into develop in this case)

git branch --merged develop | grep -v develop | xargs git branch -d
@Emuentes

This comment has been minimized.

Copy link
Owner Author

@Emuentes Emuentes commented Feb 16, 2016

@chrisng
https://gist.github.com/Emuentes/48848aaddf3e7cc3b13e

You make a good point related to both this and the previous script I posted, if I excluded origin in the git delete script, the fact that each branch name includes "origin/" would would free me of the need to use cut to remove "origin/" from the branch names before running the git delete command.

The other gist I have deletes the branches that are ready and this one just lists them.

Couple things to note:

  1. I used a regex for develop because people use "develop" or words containing "develop" in their branch names. That's why my regex includes a dollar sign after "develop" to note that only lines ENDING in "develop" should be addressed.

  2. Your script is solid for local branches, I'm working on deleting remote branches, a simple addition of the "-r" arg just before the "--merged" of your script would address that difference because the "origin" part of the branch name should tell the delete command of your script that you are targeting the remote branch.

I'll try getting that cut portion out :-)

I appreciate your feedback brotha. Have a great day.

-Edgar

@Emuentes

This comment has been minimized.

Copy link
Owner Author

@Emuentes Emuentes commented Feb 16, 2016

@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.

@chrisrng

This comment has been minimized.

Copy link

@chrisrng chrisrng commented Feb 16, 2016

:D sounds good man!

@micros123

This comment has been minimized.

Copy link

@micros123 micros123 commented Jul 13, 2016

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

This comment has been minimized.

Copy link
Owner Author

@Emuentes Emuentes commented Oct 25, 2016

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

This comment has been minimized.

Copy link
Owner Author

@Emuentes Emuentes commented Oct 25, 2016

@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