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
@chrisrng
Copy link

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
Copy link
Author

@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
Copy link
Author

@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
Copy link

:D sounds good man!

@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