-
-
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 |
@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:
-
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.
-
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
@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.
: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
Here is also a way to delete branches that have been merged (into
develop
in this case)