DEV Community

Cover image for Bitbucket – How to Restore Deleted Branch?
GitProtect Team for GitProtect

Posted on • Updated on • Originally published at gitprotect.io

Bitbucket – How to Restore Deleted Branch?

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
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
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.

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
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.

✍️ Subscribe to GitProtect DevSecOps X-Ray Newsletter – your guide to the latest DevOps & security insights

🚀 Ensure compliant DevOps backup and recovery with a 14-day free trial

📅 Let’s discuss your needs and see a live product tour

Top comments (0)