How to restore a deleted branch

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles 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 Knowledgebase 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  dropdown
  • Review 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 under Branch 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 git checkout -b <branchname>
  • Then push the branch, git push origin <branchname>

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

Last modified on Jun 3, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.