Restoring a Deleted Commit
If a specific commit was accidentally deleted (for example, through a reset or rebase), you can often restore it using the reflog or the commit hash if you know it.
1. Find the Commit Hash Using Reflog
The reflog can help you locate the hash of the deleted commit.
Steps: View the Reflog:
git reflog
Look through the reflog entries to find the commit hash of the deleted commit.
Create a New Branch or Reset:
Once you have the commit hash, you can either create a new branch pointing to that commit or reset an existing branch to that commit.
Create a New Branch:
git checkout -b <new-branch-name> <commit-hash>
Reset an Existing Branch:
Be careful with this approach as it can alter the branch history.
git reset --hard <commit-hash>
How to Change a Git Commit Message After a Push?
Accidentally deleting a branch or commit in Git can seem alarming, but Git’s powerful version control capabilities often allow you to restore them with relative ease. This guide will walk you through the steps to recover a deleted branch or commit.
Table of Content
- Restoring a Deleted Branch
- Using GitHub or GitLab
- Restoring a Deleted Commit
- Using git cherry-pick
- Conclusion