I've created lots of branches in one of our repositories. Those branches are for testing before it will be pulled to the master. Now I see lots of them on the list and they we will never use it again. How to delete those branches directly to Bitbucket?
This question is related to
git
bitbucket
git-branch
In addition to the answer given by @Marcus you can now also delete a remote branch via:
git push [remote-name] --delete [branch-name]
If you are using a pycharm IDE for development and you already have added Git with it. you can directly delete remote branch from pycharm. From toolbar VCS-->Git-->Branches-->Select branch-->and Delete. It will delete it from remote git server.
in Bitbucket go to branches in left hand side menu.
In Android Studio, the options down the right corner of the IDE:
For deleting branch from Bitbucket,
Try this command, it will purge all branches that have been merged to the develop
branch.
for i in `git branch -r --merged origin/develop| grep origin | grep -v '>' \
| grep -v master | grep -v develop | sed -E "s|^ *origin/||g"`; \
do \
git push origin $i --delete; \
done
I could delete most of my branches but one looked like this and I could not delete it:
Turned out someone had set Branch permissions
under Settings
and from there unchecked Allow deleting this branch
. Hope this can help someone.
Update: Where settings are located from question in comment. Enter the repository that you wan't to edit to get the menu. You might need admin privileges to change this.
If you like fun, then you can just go to the listing page of you branches (for example merged) and just run in the javascript console:
document.querySelectorAll('tr td div a:first-child').forEach(function(item) { fetch('https://bitbucket.org/snippets/new?owner=<yourprofilenick>', {'credentials': 'same-origin'}).then((response) => {return response.text()}).then(function(string) { return /'csrfmiddlewaretoken' value='(.*)'/g.exec(string)[1] }).then(function(csrf) { if (!~item.innerText.indexOf('/')) return;
fetch(`https://bitbucket.org/!api/2.0/repositories/<your_organization_path>/refs/branches/${item.innerText}`, {headers: {"x-csrftoken": csrf}, credentials: "same-origin", method: 'DELETE'}).then(() => console.log(`${item.innerText} DELETED!`)) }) })
<yourprofilenick>
with your BitBucket nick<your_organization_path>
with your organization pathFirst we need a page with with a CSRF token in the page source, so I choose:
https://bitbucket.org/snippets/new?owner=<yourprofilenick>
Then for each branch (in a branch listing) it gets CSRF token and deletes that branch.
Remeber to prevent sensitive branches before deleting in repo settings.
It WON'T delete the main branch.
You have to be logged in.
It deletes only branches visible on that page (so to delete the rest of branches you have to go to the next page).
git push <repository> -d <branch>
to get the repository
, type git remote -v
in command line
I've wrote this small script when the number of branches in my repo exceeded several hundreds. I did not know about the other methods (with CLI) so I decided to automate it with selenium. It simply opens Bitbucket website, goes to Branches, scrolls down the page to the end and clicks on every branch options menu -> clicks Delete button -> clicks Yes. It can be tuned to keep the last N (100 - default) branches and skip branches with specific names (master, develop - default, could be more). If this fits for you, you can try that way.
https://github.com/globad/remove-old-branches
All you need is to clone the repository, download the proper version of Chrome-webdriver, input few constants like URL to your repository and run the script.
The code is simple enough to understand. If you have any questions, write comments / create an Issue.
Source: Stackoverflow.com