Bitbucket – How to Restore Deleted Branch?
Tags: programming, coding, tutorial, devops
Published: 2024-01-04 12:11:00

You probably deleted a git branch more than once, but what will you do if it turns out that you need it afterwords. Unfortunately, it happens that somebody can delete branches. In this case, we only need to restore this branch, no matter was it deleted accidentally or intentionally, right? But is it always that easy? What if the git branch has also been removed from the main repository on the server? What is the best way to approach this problem? Should you remember anything important? And, finally what approach should you choose to solve this issue?

## The problem with deleted branches
Imagine a situation that we are working on an important project. We get up early at dawn, i.e., after 11, drink coffee, do morning exercises, eat the bucket marked Breakfast I, and sit down at our workstation. After logging in, we realize that **one of the key branches we worked on at an earlier time, which has a branch name “kafj6”, is no longer in the main repository**, which means that someone used the _git push origin –delete kaf6_ command. At this point, we check the local repo and contact the entire team to see if anyone has this branch on their local repository. In case it turns out that someone on the team has a local copy, it is enough to send them to the server using the _git push_ command.

## How to recover deleted branch?

![An example of using the git log command with the p parameter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7cs13bwrdsukygtqwgj.jpg)
**Listing 1. An example of using the git log command with the p parameter.** 
Of course, we can use git commands like _git checkout -b kaf6 commit_hash_ for our local repository typed in a command line, but what if we don’t know the hash for the last commit at this branch? We can use such commands as _git reflog_ and check each commit with the _git log_ command. In Listing 1, you can see an example of a git _log command_ with -p flag.

![Presentation of the problem and solution from the console side](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7t10deooz86ovhd9ski.jpg)
**Listing 2. Presentation of the problem and solution from the console side.**
The problem appears when **no one has a local copy of the missing local or remote branch**, and we also deleted our version using the _git branch -d kaf6_ command in a command line. At this point, we can rely on a manual search for a given local or remote branch using the _git reflog_ command or** restore a given branch from the backup**.

In the case of manual search using _reflog_ tool, we must be aware that even the Bitbucket in their official documentation recommends a full backup of our project before starting this process. This means that using [the GitProtect.io solution will allow us to reverse the lost branch with one click](https://gitprotect.io/bitbucket.html).

In Listing 2, we can see that while the procedure itself is not complex, you must be very careful when performing it. Especially if we don’t have an implemented backup system, in this case, a mistake can cause us a lot of work. You can see it perfectly with the _git reflog show_ command, which we will use when we’re not sure of the exact name of the deleted branch. In this case, we will try to “hit” which change it was, but without certainty, we can only cause problems to us and our team. In Listing 2, we can see one more curiosity. That the branch was removed with the -D flag, not the -d flag, and this is related to our example. **Because to delete a remote or local branch that is not fully merged yet, use the -D flag with _git branch -D branch_name_.**

## How to find a searched branch in a time period

![Demonstration of git diff command with a time range](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8c6gxtujdam9at4ia5x.jpg)
**Listing 3. Demonstration of _git diff_ command with a time range.**
We can narrow down the **search for a deleted branch** by searching in a given time period. However, we have to be convinced when the branch we are looking for has been deleted. For example, if we know that the missing branch was in the git repository the day before, we can use the command _git diff branch_name @ {0} branch_name@ hesive1.day.ago}_. We presented an example of using the _diff_ utility in Listing 3.

**We can restore a deleted branch mainly in the three ways** described above, but the best and sure solution will be to restore them from a backup. GitProtect.io, with their product, will help in this task! Thanks to GitProtect.io, we will ensure that our projects are always safe and that failures or unforeseen events are not a blow to us.

It’s time to move to the next topic. This time we are going to discuss the [pull request vs. merge request](https://gitprotect.io/blog/pull-request-vs-merge-request-whats-the-difference/).

✍️ Subscribe to [GitProtect DevSecOps X-Ray Newsletter](https://gitprotect.io/gitprotect-newsletter.html?utm_source=d&utm_medium=m) – your guide to the latest DevOps & security insights

🚀 Ensure compliant [DevOps backup and recovery with a 14-day free trial](https://gitprotect.io/sign-up.html?utm_source=d&utm_medium=m)

📅 Let’s discuss your needs and [see a live product tour](https://calendly.com/d/3s9-n9z-pgc/gitprotect-live-demo?month=2024-04&utm_source=d&utm_medium=m)