Recovering a deleted branch from a remote on Bitbucket (git)
Asked Answered
M

4

28

I want to recover a branch that was deleted from our remote shared repository on Bitbucket. I know that reflog is the way to go with local repositories.

How would I got about achieving this on the remote one?

Malloy answered 24/3, 2013 at 8:54 Comment(2)
Does this answer relate to your problem?Appalachian
No, since unfortunately I do not have these branches in my system's history.Malloy
T
46

Four years later...

I came across this answer because I deleted a branch through the bitbucket.org UI that a team member wanted restored.

I discovered that git branch --remote shows all the branches on origin, even the ones that are deleted through the UI.

I checked out the origin branch locally with git checkout origin/<branch_name> -b <branch_name>, then did git push -u origin <branch_name> and it showed up in the UI again.

Topple answered 4/5, 2017 at 21:28 Comment(7)
This is ridiculous, but it works. 1. Does this approach work with github as well? 2. If a branch was removed from console (not from Bitbucket UI) will we still see removed branch with git branch --remote?Bolshevist
1. Yes, this works with GitHub as well, so long as you haven't pruned the remotes. 2. If you prune remotes in Bitbucket or GitHub, that branch is gone for good, I believe.Topple
This works. I deleted a branch accidentally from bitbucket.com, and used the remotes to bring up a new one same.Albanian
This ONLY works if you have the most up-to-date local copy of the branch. If you do not, and delete the branch from bitbucket UI, this will not work. But if you have a local copy, you can just do git checkout <branch name> then git push -u origin HEAD without creating a new branch of anything. For all other cases, VonC has the right ideaVeron
This works very well. Nice tweak. Saved a lot of time and confusion.Ronaldronalda
What a life savior! Bitbucket's feature of delete after merge made me accidentally delete development branch which had 4-5 months' of whole team's work. I thought I was soo doomed.Prudential
keep in mind it will not work from a different station. If another staion is not familior with your local branch, you will not be able to access itXanthe
I
7

reflog is still the answer, except you don't have access to the reflog on the remote (Bitbucket ) side.

That means you need to write to Bitbucket support in order for them to restore what you need.

Indefinable answered 24/3, 2013 at 11:10 Comment(0)
C
1

Yes, this method totally works. But I would suggest to first check with this command before proceeding with the other two to restore branch:

git branch --remote 

If your branch is showing in the list in the output of the above command, go for these confidently:

git checkout origin/<branch_name> -b <branch_name>
git push -u origin <branch_name> 
Crinite answered 10/8, 2021 at 8:47 Comment(1)
Which method works? Are you referring to Michael's answer?Calmative
A
0

I accidentally Deleted a Branch in BitBucket Cloud.
I am disappointed that BitBucket didn't retain it as a "Deleted" Branch (recycle bin style).
Well, apparently they sort of do; they just don't show Deleted Branches from their website.

The Fix:
  1.) Change something innocuous.
        (Like adding a space in a file, where you know it will be ignored.)
  2.) Then check-in and push the branch from your local machine.
        (Preferably from wherever the last check-in to the deleted branch was made).
To my surprise it worked without issue and the branch was restored like nothing had happened.

Side Note:
Odd that in order to merge code I have to get permission from others,
but deleting code requires no oversight (at least that's how our org was set up).
Still totally my fault for mixing up my branches.
Creating branches for every single little issue we work on is new to me and a pain, but I get why.
My coworker says she waits until we go to release before cleaning up any dead branches.
Lesson learned.

Abode answered 30/3, 2023 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.