GUIDE深度 · Deep 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.