Git offers multiple features for different purposes, such as recovering files, reverting commits, restoring deleted branches, and many more. On Git, local repositories usually work on the project root branch known as “main”. You can create and switch branches using Git commands. However, you may accidentally delete a branch that can be important for your projects. This situation can be handled by performing the restore operation.
This guide will teach how to restore deleted Git branches.
How to Restore Deleted Git Branch?
Sometimes, we want to delete the merged and unmerged branches from our Git repository using the Git commands, such as the “$ git branch –merged” command and the “$ git branch –no-merged” command for listing branches. To restore those deleted Git branches, the “$ git checkout -b <branch-name> <stash-index>” is utilized.
Let’s move forward to execute these commands and understand their working!
Step 1: Move to Git Repository
Firstly, navigate to the Git local repository:
Step 2: Switch Branch
Next, switch to the existing “master” branch using the provided command:
Step 3: View Merged Branches
Execute the “git branch” command with “–merged” option to display the list of the merged branches:
As you can see, we have four merged branches, and currently we are in working in the “master” branch:
Step 4: Delete Merged Branches
Now, select any of the branches and execute the “git branch -d” command and specify its name:
Here, the “-d” denotes the operation to delete the specified branch:
Step 5: View Unmerged Branches
To view the unmerged branches of Git repository, run the following command:
According to below output, we have six unmerged branches:
Step 6: Delete Unmerged Branches
Next, to delete the unmerged branch, utilize the provided command:
As you can see, our unmerged branch named “Tag-branch” is deleted successfully:
Step 7: Check References History Log
To view the references of the History log of Git repository, run the “git reflog” command as follows:
As a result, the reference log of the whole repository will be displayed. Now, to restore a deleted branch, scroll down through the terminal, identify the history stamp, and copy its stash index:
Step 8: Restore Unmerged Branch
Now, execute the “git checkout” with the deleted branch name and copied stash index:
It can be seen that we have successfully restored the deleted branch and switched to it:
Step 9: Restore Merged Deleted Branch
To restore the merged deleted branch, again, execute the “git reflog” command and find out its history stamp and copy its stash index. For instance, we have deleted the merged “branch1” and copied its stash index:
Run the provided command to restore the “branch1” merged Git branch:
According to the below output, we have effectively restored and immediately switched the “branch1” command:
We have explained how to restore deleted Git branches.
To restore a deleted Git branch, navigate to the Git repository and switch to the “master” branch. Then, view the repository branches list utilizing the “$ git branch –merged” command for the merged branches and the “$ git branch –no-merged” command for non-merged branches. After that, check the history of the reference logs. Lastly, execute the “$ git checkout -b <branch-name> <stash-index>”. In this guide, we have talked about how to restore deleted Git branches.