Last Updated on March 15, 2024

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.

Get free trial

How to recover deleted branch?

$ 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 @@
\ No newline at end of file
\ 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 @@

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.

Disaster Recovery Use Case

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 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 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 searched branch in a time period

$ git diff master@{0} kaf6@{}

warning: log for 'kaf6' only goes back to Sat, 19 Jun 2021 12:06:29 +0200

$ git diff master@{0} master@{}

diff --git a/kafej.xml b/kafej.xml
deleted file mode 100644
index 9ca37c4..0000000
--- a/kafej.xml
+++ /dev/null
@@ -1,2 +0,0 @@
\ 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., with their product, will help in this task! Thanks to, 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.

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

Comments are closed.

You may also like