Delete branches in Bitbucket

GitBitbucketGit Branch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

If the branches are only local, you can use -d if the branch has been merged, like

git branch -d branch-name

If the branch contains code you never plan on merging, use -D instead.

If the branch is in the upstream repo (on Bitbucket) you can remove the remote reference by

git push origin :branch-name

Also, if you're on the Bitbucket website, you can remove branches you've pushed by going to the Feature branches tab under Commits on the site. There you'll find an ellipsis icon. Click that, then choose Delete branch. Just be sure you want to drop all the changes there!

enter image description here

Solution 2 - Git

For deleting branch from Bitbucket,

  1. Go to Overview (Your repository > branches in the left sidebar)
  2. Click the number of branches (that should show you the list of branches)
  3. Click on the branch that you want to delete
  4. On top right corner, click the 3 dots (besides Merge button).
  5. There is the option of "Delete Branch" if you have rights.

Solution 3 - Git

In addition to the answer given by @Marcus you can now also delete a remote branch via:

git push [remote-name] --delete [branch-name] 

Solution 4 - Git

I could delete most of my branches but one looked like this and I could not delete it:

enter image description here

Turned out someone had set Branch permissions under Settings and from there unchecked Allow deleting this branch. Hope this can help someone.

enter image description here

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.

enter image description here

Solution 5 - Git

in Bitbucket go to branches in left hand side menu.

  1. Select your branch you want to delete.
  2. Go to action column, click on three dots (...) and select delete.

Solution 6 - Git

Step 1 : Login in Bitbucket

Step 2 : Select Your Repository in Repositories list. enter image description here

Step 3 : Select branches in left hand side menu. enter image description here

Step4 : Cursor point on branch click on three dots (...) Select Delete (See in Bellow Image) enter image description here

Solution 7 - Git

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.

Solution 8 - Git

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.

Solution 9 - Git

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

Solution 10 - Git

git push <repository> -d <branch>

to get the repository, type git remote -v in command line

Solution 11 - Git

In Bitbucket go to your project, click branches , click on the three points and click delete multiple.

This option for masse delete.

enter image description here

Solution 12 - Git

In Android Studio, the options down the right corner of the IDE:

  • Change/checkout other local branch
  • Delete unwanted local branches (i.e. v0.0.1...)
  • Delete unwanted remote branches (i.e. origin/v0.0.1...) -- this step will delete branches in BitBucket if the branches are not prevented to be deleted and they are not the MAIN BRANCH.

Solution 13 - Git

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!`)) }) })

BEFORE RUN

  • replace <yourprofilenick> with your BitBucket nick
  • replace <your_organization_path> with your organization path

HOW IT WORKS

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

BEWARE

Remeber to prevent sensitive branches before deleting in repo settings.

It WON'T delete the main branch.

ADDITIONAL INFO

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

Solution 14 - Git

In bitbucket web console., delete branch is disabled when there are active Pull requests.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncatherineView Question on Stackoverflow
Solution 1 - GitMarcusView Answer on Stackoverflow
Solution 2 - GitMital PritmaniView Answer on Stackoverflow
Solution 3 - GitRalf Rafael FrixView Answer on Stackoverflow
Solution 4 - GitOgglasView Answer on Stackoverflow
Solution 5 - GitPrashant SharmaView Answer on Stackoverflow
Solution 6 - GitNanhe KumarView Answer on Stackoverflow
Solution 7 - GitDanView Answer on Stackoverflow
Solution 8 - GitJyoti AmageView Answer on Stackoverflow
Solution 9 - GitEhab Al-HakawatiView Answer on Stackoverflow
Solution 10 - GitMJ MontesView Answer on Stackoverflow
Solution 11 - Gitmohamed stitaneView Answer on Stackoverflow
Solution 12 - GitTim LongView Answer on Stackoverflow
Solution 13 - GitturkusView Answer on Stackoverflow
Solution 14 - Gitvijeth.agView Answer on Stackoverflow