GUIDEDeep dive
Undo and Recover in Git
Git undo has three layers: working tree, staging area and history. Identify the layer first, then choose restore, reset or revert; prefer revert for pushed commits and avoid rewriting public history.
Choose by layer
Working tree: git restore file.
Staging: git restore --staged file.
Local commits: git reset --soft/--mixed/--hard.
Pushed commits: git revert.
Recover lost work
Use git reflog to find the HEAD position.
Use git reset --hard HEAD@{n} to return there.
FAQ
Can I recover after reset --hard?
Uncommitted changes are usually lost; committed work may be recoverable with reflog. Back up first.
revert vs reset?
Revert creates an inverse commit and is safe; reset rewrites history and suits local work.
Sources
Related calculators
Git Commands: Gamified 60-Card Deck60 Git command and concept cards covering commits, branches, undo and recovery, with spaced review.More guides
Hiragana vs KatakanaRead guideSpaced Repetition and the Forgetting CurveRead guideOn Readings vs Kun ReadingsRead guideAn N5 Kanji Study OrderRead guideKEEP READING
Keep reading
Read in order, or jump to another guide on the same topic.
← PreviousDaily Git WorkflowThe status, diff, add, commit, pull and push loop.Next →Excel Formula BasicsReferences, absolute/relative, errors and IF.