Beginner · 6 min read
restore, reset, revert: which one?
Three commands that “undo”, three very different results. The cheat sheet you'll come back to.
restore: undo changes in files
Puts a file back to its last committed (or staged) state. Nothing about history changes. Without --staged, your uncommitted edits to that file are lost.
$ git restore --staged path/to/file$ git restore path/to/file
reset: move the branch backwards
Moves your branch to an older commit. --soft keeps the changes staged, --mixed (default) keeps them unstaged, --hard throws them away. It rewrites history: local commits only.
$ git reset --soft HEAD~1
revert: cancel a commit publicly
Creates a new commit that does the opposite of an old one. History is preserved, so it is safe on shared branches. When in doubt on main, revert.
$ git revert <commit>