Restore a deleted Bitbucket branch
A branch is only a name for its last commit. To bring a deleted branch back, find the hash of that commit in a clone, make a branch from it and push it to Bitbucket. This is the way Atlassian's knowledge base describes for Bitbucket Cloud and Data Center (How to restore a deleted branch). Copy the clone's folder first, as Atlassian suggests, and don't wait long: Git cleans up commits that nothing points to.
Find the last commit
Try these in order, in the clone where you last had the branch. The examples use the branch feature/one.
- You just deleted it.
git branch -dprinted the hash:Deleted branch feature/one (was 773677e). - It is still a remote-tracking branch. If this clone hasn't fetched with pruning since the branch went from Bitbucket, it still has
origin/feature/one:git branch -r git rev-parse origin/feature/one - The reflog. Git logs where HEAD has been. The line where you moved to the branch has its hash:
git reflog --no-abbrev | grep feature/one - A dangling commit. Commits no branch points to any more:
Check that a hash is the one you want withgit fsck --lost-foundgit cat-file -p <hash>andgit log <hash>. - Another clone. A teammate's clone, or a build machine's, may still have the branch; push it from there.
- Its pull request. If the branch had a pull request that was merged with a merge commit or fast-forward, its commits are in the destination branch, and the pull request's Commits tab shows the newest one's hash. After a squash merge they are not: only the squashed commit is.
To review the branch's pull request once it is back: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, lists the pull requests that wait for your review, with their diffs, in the IDE.
Make the branch again and push it
git checkout -b feature/one 773677e
git push -u origin feature/one
The short hash works in most commands; if one doesn't take it, use the full hash. A pull request that was declined or merged stays as it was: open a new one from the restored branch if it still needs review.
Keep a branch from being deleted
In Bitbucket Cloud, a branch restriction (Repository settings > Branch restrictions) keeps the branch from being deleted unless its rule has Allow deleting this branch checked, which Atlassian presents as a guard against someone deleting the main branch (Use branch permissions). For a source branch deleted by a merge, see whether Close source branch was checked on the pull request.
Bitbucket pull requests in IntelliJ IDEA
Pull Request Inbox for Bitbucket, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pull requests that wait for your review in an IDE tool window, with their diffs and discussions, and shows next to each which merge checks pass and which block the merge. The review inbox, diffs, discussions and merge checks are free; approving, commenting and merging are Pro, with a 30-day trial. It also works with Bitbucket Data Center (beta).
Get it on JetBrains Marketplace
Bitbucket and Atlassian are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Bitbucket Cloud branch restrictions · Changes missing from a branch · The source branch was not deleted after the merge · Check out a pull request locally · Bitbucket merge strategies · 日本語