Restoring a Deleted Branch
Step 1: Find the Branch’s Last Commit
First, use git reflog to find the commit at the deleted branch’s tip. git reflog shows a log of all changes to the repository’s branches and can be used to locate the SHA-1 hash of the commit.
1. Open your terminal or command prompt.
2. Navigate to your repository.
3. Run git reflog:
git reflog
This command will display a list of recent actions, including those that affected the branch you deleted. Look through the list to find the commit hash (a long string of characters) associated with the deleted branch.
Step 2: Create a New Branch from the Commit
Once you have identified the commit hash, you can create a new branch pointing to it.
1. Copy the commit hash.
2. Create a new branch from that commit:
git checkout -b <new-branch-name> <commit-hash>
Example:
git checkout -b restored-branch abc1234
This command creates and switches to a new branch named restored-branch at the commit abc1234.
How to Restore a Deleted Branch or Commit with Git?
Deleting a branch or a commit in Git might seem like a disaster, especially if it was done unintentionally. However, Git provides robust tools to help you recover deleted branches or commits. Here’s a detailed guide on how to restore a deleted branch or commit in Git.
Table of Content
- Understanding the Basics
- Restoring a Deleted Branch
- Restoring a Deleted Commit
- Important Points
- Conclusion