This question is in reference to Atlassian Documentation: Using branches in Bitbucket Server
As some developers tend to forget to delete their branches after they have been merged, we end up with a lot of stale branches that need to get deleted. Is there a way for me to delete all of the branches that were last modified before a given date? To prevent this problem from happening again, if you have any suggestions on how to manage so that users delete their branches after merging (the checkbox asking if they want to delete the branch seems to get overlooked).
I presume you want to delete all branches that are already merged into 'master'?
You can do it from the command-line using a combination of 'git branch --merged origin/master' alongside the unix 'xargs' utility:
# Make sure remotes are up to date (with stale remotes purged): git fetch -p # Initial no-op run --- do the branches to delete look correct? # Be careful to omit 'master' from the output. git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs # Do the bulk delete!!! (can take a long time...) git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs git push --delete origin
Note: each line of output from "git branch --remote" will begin with " origin/" and so the "cut -b 10-" removes that. If your remote is named something other than "origin" you will need to adjust the value "10" in that cut command.
The problem with this approach is that sometimes important branches like 'develop' or 'production' or 'release' might have been merged without any subsequent activity, and so they will get toasted by this.
I recommend installing my free add-on first (Commit Graph for Bitbucket Server) and reviewing all the merged branches first before running the delete. It's a great way to visually confirm that you're not deleting anything important. Just uncheck all the options except "master" on the "All Branches Graph", hit reload, and scroll down to visually inspect the branches before deleting.
Here's a live demo of the "All Branches Graph" in action with only 'master' checked: vm.bit-booster.com/bitbucket
Any branches that you want to keep you can remove from the command above with "grep -v" additions after the initial "grep -v master" component (e.g., grep -v 'master' | grep -v 'keep-this' | grep -v 'and-keep-this-too' | etc...).
I don't see the difference between the first and second line:
# Initial no-op run --- do the branches to delete look correct? # Be careful to omit 'master' from the output. git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs # Do the bulk delete!!! (can take a long time...) git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs
Neither can Chrome in Ctrl+F. Can you help me how is one no-op run and the other the actual action?
I case you don't want to install any add-ons from marketplace you have another option.
I've wrote this small script when the number of branches in my repo exceeded several hundreds. I did not know about the first method so I decided to automate it with selenium. If this fits for you, you can try that way.
All you need is to clone this repository, download the proper version of Chrome-webdriver, input few constants like URL to your own repository and run the script.
The code is simple enough to understand. If you have any questions, write comments / create an Issue.
Best way is to sense developers to delete their branches immediately after merge if all work for that branch is done. The reviewer may check this. Make this to your QA process.
To clean up your current branches, the plugin "My Branches" may help. It determines your branch contributions, lists them, and lets you delete outdated branches easily. You can delete multiple branches in bulk.
If you have any questions about that this plugin, don't hesitate to contact me.
i have a shell script which will delete all the branches that has no commits in the past 60 days, this was implemented as part for gitclean up activity.
After purging these branches in git the first generator job takes time approx 2-3 hours due to the references of the deleted branches in bitbucket. Do you know a way to get rid of these references when the git branches are being purged ?
We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...
Connect with like-minded Atlassian users at free events near you!Find an event
Connect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.Host an event
You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events