You probably deleted a branch more than once, but what will you do if it turned out to be needed. In this case, we only need to restore this branch, right? But is it always that easy? What if the branch has also been removed from the main repository on the server? What is the best way to approach this problem, what to remember, and what approach to solving this problem should you choose?

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, named “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 repository 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?

$ git log -p f3dadab

Author: Kafej <>
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 <>
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 commands like git checkout -b kaf6 commit_hash for our local repository, but what if we don’t know the hash for the last commit at this branch? We can use 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 branch, and we also deleted our version using the git branch -d kaf6 command. At this point, we can rely on a manual search for a given 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 for 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 branch that is not fully merged yet, use the -D flag with git branch -D branch_name.

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, but 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 repository the day before, we can use the command git diff branch_name @ {0}}. An example of using the diff utility is presented 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.

Comments are closed.

You may also like