Bitbucket – Restore Deleted Branch Tips
Last Updated on October 30, 2024
You probably deleted a git branch more than once, but what will you do if it turns out that you need it afterward? Unfortunately, it happens that somebody can delete branches. In this case, we only need to restore this branch, no matter whether was it deleted accidentally or intentionally, right?
But it is not always that easy. What if the git branch has also been removed from the remote repository? 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?
A sneak peek at deleted branches
Deleted branches can become a great frustration for developers, especially if those branches contain important commits before they are merged with other branches. Moreover, maintaining a healthy Git repository requires an understanding of deleted branches and the ways to recover them.
Well, a branch in Git is basically a reference to a particular commit. For example, when a branch is deleted, this reference is removed. Hence, commits still remain in the repository, but become unreachable… It may even seem that they are lost. Hovewer, hopefully, there is a way to recover these deleted branches and restore your work.
The problem with deleted branches
Imagine a situation in which 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. Additionally, we check the repository directory for backups that might help recover the deleted branch. 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.
💡 Do you know that identifying and locating the deleted branch is the first step in recovering it? There are several ways to do this – use Git reflog, Git log, or manual inspection of commits.
How to recover deleted branch (Bitbucket)?
$ git log -p f3dadab
Author: Kafej <[email protected]>
Date: Sat Jun 19 12:01:53 2021 +0200
add changes to line 2
diff --git a/kafej.xml b/kafej.xml
index f735f80..9ca37c4 100644
--- a/kafej.xml
+++ b/kafej.xml
@@ -1 +1,2 @@
-kafej
\ No newline at end of file
+kafej
+KafejDel
\ No newline at end of file
commit 1b202f57fefc235e7fedd59ed84e4551c90551d9
Author: Kafej <[email protected]>
Date: Sat Jun 19 11:57:18 2021 +0200
Change at line 1
diff --git a/kafej.xml b/kafej.xml
new file mode 100644
index 0000000..f735f80
--- /dev/null
+++ b/kafej.xml
@@ -0,0 +1 @@
+kafej
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.
$ git init
#Deleting local branch
$ git branch -D kaf6
Deleted branch kaf6 (was f3dadab).
#Searching for commit hash
$ git reflog
1b202f5 (HEAD -> master) HEAD@{0}: checkout: moving from kaf6 to master
f3dadab HEAD@{1}: commit: add changes to line 2
1b202f5 (HEAD -> master) HEAD@{2}: checkout: moving from master to kaf6
1b202f5 (HEAD -> master) HEAD@{3}: commit: Change at line 1
8c9440d (checkout) HEAD@{4}: reset: moving to 8c9440d
8c9440d (checkout) HEAD@{5}: checkout: moving from kaf6 to master
8c9440d (checkout) HEAD@{6}: checkout: moving from master to kaf6
8c9440d (checkout) HEAD@{7}: reset: moving to 8c9440d52b42424ff9d40947c66639ed12a7d1e8
a8aa0f8 HEAD@{8}: reset: moving to a8aa0f86c48a89b89819bbd7065c79dd186ea7ce
a8aa0f8 HEAD@{9}: commit: add kafej.xml
8c9440d (checkout) HEAD@{10}: revert: Revert "Add file"
0672413 HEAD@{11}: commit (initial): Add file
$ git reset --hard f3dadab
HEAD is now at f3dadab add changes to line 2
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 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. To delete a remote or local branch that is not fully merged yet, use the -D flag with git branch -D branch_name.
How secure are your repos and metadata? Don’t push luck – secure your code with the first professional GitHub, Bitbucket, and GitLab backup.
How to find a searched branch in a time period?
$ git diff master@{0} kaf6@{1.day.ago}
warning: log for 'kaf6' only goes back to Sat, 19 Jun 2021 12:06:29 +0200
$ git diff master@{0} master@{1.day.ago}
diff --git a/kafej.xml b/kafej.xml
deleted file mode 100644
index 9ca37c4..0000000
--- a/kafej.xml
+++ /dev/null
@@ -1,2 +0,0 @@
-kafej
-KafejDel
\ No newline at end of file
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} [email protected]}. 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.
[FREE TRIAL] Ensure compliant Bitbucket backup and Disaster Recovery with a 14-day trial 🚀
[CUSTOM DEMO] Let’s talk about how backup & DR software for Bitbucket can help you mitigate the risks
How to prevent accidental deletion of Git branches
Well, it’s not a secret that it’s better to prevent the catastrophe than deal with it. So, let’s look at some tips that can help you prevent accidental deletion:
- use Git hooks, which can help you prevent the deletion of certain branches, e.g. you can set a pre-delete hook to block the deletion of a defined branch.
- use permission management, which can help you limit who has permission to remove branches from your repository, preventing accidental or unauthorized deletions,
- have a reliable backup strategy for your Bitbucket ecosystem, which allows you to have regular automated backups and ensure that you can restore a deleted branch and any critical Bitbucket data from accidental deletions or other events of disaster,
- educate your team – train your team on how to use Git and other DevOps tools safely,
Before you go:
🔎 Find out which Bitbucket backup best practices to follow to make your critical data accessible and recoverable in any event of failure
🌟 See in practice! Read how the Wharton School of the University of Pennsylvania adopted GitProtect backups for Bitbucket to ensure its data protection
👀 Check out what security incidents and vulnerabilities Atlassian faced in 2023 and how the service provider was dealing with them
✍️ Subscribe to GitProtect DevSecOps X-Ray Newsletter and always stay tuned with the latest DevSecOps insights
📅 Schedule a live custom demo and learn more about GitProtect backups for your Bitbucket ecosystem
📌 Or try GitProtect backups for Bitbucket repositories and metadata to eliminate data loss and guarantee business continuity