How to restore a deleted branch in Bitbucket Data Center
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
This knowledge base article describes alternative ways to restore a deleted branch in Bitbucket Data Center.
Solution
Solution 1 - Push the branch from a local copy
If a user has a local copy of the repository, that person can just push the deleted branch back to the remote repository.
Solution 2 - Review the Push logs in Bitbucket and recreate the branch from the UI
Push logs record pushes to repositories, as well as branch creation and deletion events. This feature is available starting from Bitbucket version 7.1.
Using Push logs:
Open the push logs for the repository
Select the deleted branch from the
Branch
dropdownReview the push logs to determine the last commit in the branch before it was deleted
Sample
Click the commit hash in the push logs to open the commit on a new page
Create a tag for the commit.
Sample
Recreate the branch based from the tag created in the previous step.
Open the
Create Branch
page then choose the tag underBranch from > Tags
Sample
Set the branch name and click
Create branch
.
Solution 3 - Review the git reflog and recreate the branch from a local copy
Using Git:
First, on your local repository, perform
git reflog
. This will list down all previous HEADs. Find the latest commit hash for the branch that you have deleted.Once you have the commit hash, perform
git reset --hard <commit>
. This will revert the HEAD back to the latest commit on the branch.Recreate the branch again and checkout to that new branch with
git checkout -b <branchname>
Then push the branch with
git push origin <branchname>
⚠️ Important note: please take a full backup before performing these steps.
Related Link
For instructions on how to detect deleted branches, refer to the How to identify a deleted branch page.
Was this helpful?