21

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?

OpherV
  • 6,627
  • 4
  • 33
  • 55

3 Answers3

33

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.

Michael
  • 745
  • 5
  • 12
  • 1
    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? – Yury Bondarau Apr 03 '18 at 08:31
  • 1
    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. – Michael Apr 18 '18 at 01:09
  • This works. I deleted a branch accidentally from bitbucket.com, and used the remotes to bring up a new one same. – Pranav Nandan Apr 27 '18 at 14:53
  • 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 ` then `git push -u origin HEAD` without creating a new branch of anything. For all other cases, VonC has the right idea – C0D3LIC1OU5 Apr 18 '19 at 17:10
  • This works very well. Nice tweak. Saved a lot of time and confusion. – Nishad K Ahamed Jun 03 '19 at 06:34
  • 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. – Arun Gowda Jan 10 '22 at 14:29
5

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.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
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> 
Aishee
  • 95
  • 1
  • 8