Git & GitHub Interview Question #58
How can you recover a commit after an accidental git reset?
Undo & Recovery, Tags, Forks, Pull Requests & Code Review Mid-Level Intermediate
Quick Interview Answer
If the commit was recently reachable in the local repository, git reflog can often reveal its previous commit ID. After identifying the correct commit, create a branch pointing to it or reset the intended branch carefully.
Detailed Explanation
If the commit was recently reachable in the local repository, git reflog can often reveal its previous commit ID. After identifying the correct commit, create a branch pointing to it or reset the intended branch carefully.
Recovery is easiest before aggressive cleanup occurs, so avoid making unnecessary destructive changes after discovering the mistake.
Code Example
git reflog
# Preserve the old commit first
git branch recovery <old-commit-id>
# Then inspect it
git log recovery --oneline