Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Undo a Git commit: reset, revert and restore

Committed too quickly? Pushed a bug? Don't panic. Git offers several ways to undo. This page explains which one to use depending on your situation.

Which command should you use?

Uncommitted changes

git restore

You modified files but haven't committed yet. You want to go back to the previous version.

git restore <file>
Local commit (not pushed)

git reset

You committed but haven't pushed yet. You want to undo the commit (with or without the changes).

git reset --soft HEAD~1
Already pushed commit

git revert

The commit is already shared. You want to undo it cleanly without rewriting history.

git revert <hash>

The 3 methods in action

Each method illustrated with a concrete example in the terminal.

git reset --soft

git revert

git restore

Comparison: reset vs revert vs restore

Criteriagit resetgit revertgit restore
Acts onCommitsCommitsFiles
Rewrites historyYesNoNo
Safe for shared branchNoYesYes
Keeps changesDepends on mode (--soft/--hard)Creates a new commitDiscards changes
Use caseLocal commit to fixPushed commit to undoModified file to restore
A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about undoing commits

Learn to undo without panicking

GitQuest trains you to master reset, revert and restore in realistic situations.

Start practicing now