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.

更新 2026-10-30 · 公式可核验

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 Commands60 Git command and concept cards in six levels, reviewed at 1/3/7/14/30/60/120-day intervals.

More guides

Daily Git WorkflowRead guide