Skip to main content

Git Undo Cheatsheet — reset / revert / restore

Git undo operations are among the most frequently used — and most confusing — commands for developers. This guide compares reset, revert, and restore across different scenarios so you always know how to undo your last action.

Updated: 2026-07-15·19 commands

Quick Decision Guide

ScenarioCommandRecommendation
Undo last commit, keep changes to redogit reset --soft HEAD~1⭐⭐⭐⭐⭐
Completely discard last commit's changesgit reset --hard HEAD~1⭐⭐ Use with caution
Undo a pushed commit safelygit revert ⭐⭐⭐⭐⭐
Discard uncommitted changes in working treegit restore ⭐⭐⭐⭐⭐
Unstage a file (keep changes)git restore --staged ⭐⭐⭐⭐⭐

Core Commands

### git reset — Move HEAD Pointer

reset has three modes, differing in how they affect the staging area and working directory:

ModeHEADStagingWorking DirSafety
--soft✅ Moves✅ Kept✅ Kept🟢 Safe
--mixed (default)✅ Moves❌ Cleared✅ Kept🟡 Caution
--hard✅ Moves❌ Cleared❌ Discarded🔴 Dangerous
### git revert — Safe Undo (Recommended)

revert doesn't rewrite history — it creates a new commit that inverses the changes from a previous commit. This is the only safe way to undo pushed commits.

``bash # Undo a single commit git revert abc1234

# Undo a range of commits git revert HEAD~3..HEAD

# Revert without auto-committing (review first) git revert --no-commit abc1234 `

### git restore — Discard Uncommitted Changes

`bash # Discard working directory changes for a file git restore src/app.ts

# Discard all working directory changes git restore .

# Unstage a file (keep working directory changes) git restore --staged src/app.ts ``

Undo(19)

CommandLevel
git commit --amend
Amend the most recent commit message or content
Basic
git reset --soft
Soft reset, keep changes staged
Intermediate
git reset --mixed
Mixed reset, keep changes in working directory
Intermediate
git reset --hard
Hard reset, discard all changes
Intermediate
git revert
Revert the changes introduced by a commit (creates a new commit)
Intermediate
git revert --no-commit
Revert changes without auto-committing
Intermediate
git restore
Restore working tree files to a specified state
Basic
git restore --staged
Unstage files from the staging area
Basic
git restore .
Restore all working tree files (discard all changes)
Basic
git restore --source
Restore files from a specific commit
Intermediate
git checkout --
Discard changes in working directory files
Basic
git checkout -f
Force switch branches discarding local changes
Basic
git clean -fd
Force remove untracked files and directories
Intermediate
git clean -n
Dry-run to show which untracked files would be removed
Basic
git clean -fdX
Remove ignored files and directories
Intermediate
git reflog
Show the reference log (all HEAD movement history)
Intermediate
git rebase --abort
Abort an in-progress rebase operation
Intermediate
git cherry-pick --abort
Abort an in-progress cherry-pick operation
Intermediate
git bisect reset
Reset the bisection state
Intermediate

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-15.