Git is a powerful version control system, but sometimes mistakes happen. Whether it’s accidentally deleting files, losing commits, or making unwanted changes, Git provides several ways to recover lost work and fix errors.
Git recovery refers to methods used to recover lost commits, restore files, or undo undesired changes using Git’s history and internal references.
git reflog shows the history of HEAD movements and helps recover commits removed from branch history.
// Show history of HEAD movements
git reflog
// Reset repository back to a lost commit
git reset --hard abc1234
// Create a recovery branch from lost commit
git checkout -b recovery-branch abc1234
// Restore a deleted file from a specific commit
git checkout <commit-hash> <path-to-file>
// Discard changes and restore last committed version
git checkout -- <file-name>
// Revert file to last committed state
git checkout -- <file-name>
// Unstage file without losing changes
git reset <file-name>
// Undo last commit but keep changes staged
git reset --soft HEAD~1
// Completely reset repository to a commit
git reset --hard <commit-hash>
git reflog before assuming data is lostgit reset --hard