Git Recovery
What is Git Recovery?
Git recovery means getting back lost commits, branches, or files.
Git keeps a record of recent changes so you can undo mistakes, even after a reset or delete.
When to Use Git Recovery
Use Git recovery when you:
- Accidentally delete a branch or file
- Reset your branch to a previous commit and lose changes
- Need to recover lost commits or changes
Recover Lost Commits with git reflog
git reflog records changes to the tip of branches and lets you find lost commits.
Example: Show Reflog
git reflog
e56ba1f (HEAD -> main) HEAD@{0}: commit: Revert "Just a regular update, definitely no accidents here..."
52418f7 HEAD@{1}: commit: Just a regular update, definitely no accidents here...
9a9add8 (origin/main) HEAD@{2}: commit: Added .gitignore
81912ba HEAD@{3}: commit: Corrected spelling error
3fdaa5b HEAD@{4}: merge: Merge pull request #1 from w3schools-test/update-readme
836e5bf HEAD@{5}: commit: Updated readme for GitHub Branches
...
Try it Yourself »
Find the commit hash you want to recover from the list.
Restore a Deleted Branch
If you deleted a branch but the commits are still in reflog, you can recreate it:
Example: Restore a Branch
git checkout -b branch-name <commit-hash>
Switched to a new branch 'branch-name'
Try it Yourself »
This brings back the branch at the commit you specify.
Recover a Deleted or Changed File
If you deleted or changed a file and want to get it back, use git restore:
This brings back the file from the latest commit.
Recover from a Hard Reset
If you used git reset --hard and lost commits, you can use the reflog to find and restore them:
Example: Undo a Hard Reset
git reflog
9a9add8 (HEAD -> main, origin/main) HEAD@{0}: reset: moving to HEAD~2
e56ba1f HEAD@{1}: commit: Revert "Just a regular update, definitely no accidents here..."
52418f7 HEAD@{2}: commit: Just a regular update, definitely no accidents here...
9a9add8 (HEAD -> main, origin/main) HEAD@{3}: commit: Added .gitignore
81912ba HEAD@{4}: commit: Corrected spelling error
3fdaa5b HEAD@{5}: merge: Merge pull request #1 from w3schools-test/update-readme
...
git reset --hard HEAD@{1}
HEAD is now at e56ba1f Revert "Just a regular update, definitely no accidents here..."
Try it Yourself »
The reset is the newest entry, HEAD@{0}. The entry before it, HEAD@{1}, is where the branch was just before the reset, so resetting to it brings back the lost commits.
Tips & Best Practices
- Regularly commit your changes to avoid losing work
- Use
git reflogto find lost commits - Use
git restoreto recover deleted or changed files