Git
Git Tutorial for Beginners with Examples
Install Git
Add Local Project to Remote Git Repository
How to Clone a Project from a Remote Git Repository
Create a New Branch in Git
Switching Branches in Git
How to Push Code from Local to Remote Repository
Pushing a New Local Branch to a Remote Git Repository
Deleting a Git Branch Both Locally and Remotely
Restoring a Deleted Git Branch
Switching to a Different Branch Without Committing Changes in Local Git Repository
Git Stash Revert: How to Restore Stashed Changes in Git
Creating a Git Pull Request
Cancel a Git Pull Request
Tags in Git
Git Commands for Beginners
Generating a Personal Access Token on GitHub
Update the Local Branch with the Latest Code Changes from the Remote Branch
Compare Two different Git Branches in Visual Studio Code
Restoring a Deleted Git Branch
- Last updated Apr 25, 2024
This solution works only if too much time hasn't been passed since the branch was deleted.
Follow these steps to restore a deleted branch in Git:
- Use 'git reflog' command to display the reference logs, which provide a history of all references such as branches, commits, and more that were updated in your Git repository. It shows a detailed log of changes to your repository's references, even if they were moved, deleted, or altered in other ways:
- Identify the sha commit hash of the deleted branch from the local records of sha.
- After you have identified the commit hash, you can follow the steps mentioned earlier to create a new branch at that commit and restore the deleted branch using the following command:
git reflog
Next, you should see something like this in your terminal:
Deleted branch my_branch_name (was 5756a8c06).
git checkout -b <branch-name> <sha>
Example: git checkout -b my_branch_name 5756a8c06