Git Reference
Git Reference / Undoing things

09Undoing things

Pick the tool by what you want to undo and whether it’s been pushed.

I want to…CommandSafe after push?
Discard edits to a filegit restore file.txt—
Unstage a filegit restore --staged file.txt—
Fix the last commit message / add a forgotten filegit commit --amendNo
Undo last commit, keep changes stagedgit reset --soft HEAD~1No
Undo last commit, keep changes unstagedgit reset HEAD~1No
Throw away last commit and its changesgit reset --hard HEAD~1No
Undo a pushed commitgit revert <sha>Yes ✓
Revert a merge commitgit revert -m 1 <merge-sha>Yes ✓
Delete untracked filesgit clean -fd (preview: -n)—
Reset branch to match remotegit reset --hard origin/main—
Restore a file from another commitgit restore --source=HEAD~3 file.txt—
git reset modeMoves branchStaging areaWorking dir
--soft✓keptkept
--mixed (default)✓resetkept
--hard✓resetreset ⚠