December 5, 2026

git reset vs revert vs restore: Which One Undoes What, Tested in a Throwaway Repository

Three erasers beside a pencil-drawn branching diagram on graph paper

Three Git commands start with “re” and all of them undo something. One rewrites history, one adds to it, and one never touches it. Picking the wrong one is how a teammate’s pull turns into a mess, or how an afternoon of uncommitted work disappears without a trace. Every command below was run in a scratch repository with Git 2.43, the version in Ubuntu 24.04, and the outputs shown are the real ones.

Video: HenrikM Dev on YouTube

The one-paragraph version

Straight from the manual

The git(1) manual has a short section on exactly this confusion. git revert makes a new commit that reverses changes made by earlier commits. git restore puts files in the working tree back from the index or from another commit, and does not update the branch. git reset moves the branch tip to add or remove commits, which changes history, and it can also restore the index, overlapping with restore.

As a table

Command Moves the branch? Changes history? Touches files? Safe on pushed commits?
git restore No No Yes, the paths named Yes
git revert Forward, by one new commit Adds to it Yes, by undoing a commit’s changes Yes
git reset Yes, to any commit Rewrites it Depends on the mode No
Cork board

git restore: files only, branch untouched

Throwing away an edit

The test repository started with four commits, each adding a line to app.txt. An accidental edit, then a restore:

$ echo "oops" >> app.txt
$ git status -s
 M app.txt
$ git restore app.txt
$ git status -s
$ tail -1 app.txt
line 4

The edit was gone and the status was clean. The git-restore manual says that without --staged, contents come from the index; with --staged, they come from HEAD.

Unstaging without losing the edit

$ echo "staged edit" >> app.txt
$ git add app.txt
$ git status -s
M  app.txt
$ git restore --staged app.txt
$ git status -s
 M app.txt

The M moved from the left column to the right: no longer staged, still changed on disk. This is the modern replacement for git reset HEAD app.txt, and it does the same thing with a clearer name.

Pulling an old version of a file

$ git restore --source=HEAD~2 app.txt
$ cat app.txt
line 1
line 2
$ git status -s
 M app.txt

The file went back two commits while the branch stayed where it was, so the change shows up as an ordinary uncommitted edit that can be committed or thrown away. The 2.43 manual still carries a warning that the command is experimental and its behavior may change, though it has been part of Git for years and the core options above are the ones the manual documents.

git revert: undo by adding

A clean revert

A second scratch repository had four commits, each adding a separate file: add a, add b, add c, add d. Reverting the second one:

$ git revert --no-edit HEAD~2
[main f695338] Revert "add b"
$ git log --oneline | head -2
f695338 Revert "add b"
12d10a8 add d
$ ls
a.txt  c.txt  d.txt

b.txt disappeared, every later commit stayed, and the history gained one commit that explains itself. Anyone who already pulled can pull again with no surprises. That is why revert is the right tool for a mistake that has already been pushed to a shared branch.

A revert that conflicts

The first repository told a different story. Reverting “commit 3”, whose only change was adding line 3 right above the line 4 added later, stopped with a conflict:

$ git revert --no-edit HEAD~1
...
$ git status -s
UU app.txt
$ cat app.txt
line 1
line 2
<<<<<<< HEAD
line 3
line 4
=======
>>>>>>> parent of 0e733f1 (commit 3)

Revert works by applying the opposite of a commit’s diff to the current code. When later commits edited the same lines or the lines touching them, Git cannot be sure what “opposite” means any more and hands the decision back. The fix is to edit the file into the intended shape, git add it and run git revert --continue. Or git revert --abort, which in the test put the repository back exactly as it was.

Railway

git reset: moving the branch, three ways

The modes, per the manual

The git-reset manual defines the three common modes. --soft does not touch the index or the working tree and leaves the changes staged. --mixed, the default, resets the index but keeps the files, so changes are kept but not staged. --hard resets both, and any changes to tracked files in the working tree since the target commit are discarded.

Tested on the same commits

Each mode was run as git reset <mode> HEAD~2 from the same starting point, the commit “Revert add b” on top of “add d”:

Mode Branch now at git status -s afterward
--soft add c D b.txt and A d.txt, both staged
--mixed add c D b.txt unstaged, ?? d.txt untracked
--hard add c Clean, except a pre-existing untracked file

With --soft, the two undone commits’ combined changes sat staged and ready to recommit as one, the standard way to squash the last few commits. With --mixed, the same changes were there but unstaged, and the file the undone commit had added showed up as untracked. With --hard, everything since “add c” vanished from the working tree.

What –hard really threw away

Before the hard reset, the test added a line, unsaved work, to a.txt without committing it, and created an untracked file. After git reset --hard HEAD~2:

$ cat a.txt
a content
$ ls
a.txt  b.txt  c.txt  untracked.txt

The uncommitted line was gone. The untracked file survived, because reset only manages tracked files. That distinction is the whole danger of --hard. Committed work can almost always be recovered. Uncommitted edits to tracked files cannot.

The safety net: reflog

Every move is written down

Git records every position HEAD has held in the reflog. After the experiments above:

$ git reflog | head -4
e940acd HEAD@{0}: reset: moving to HEAD~2
f695338 HEAD@{1}: reset: moving to ORIG_HEAD
e940acd HEAD@{2}: reset: moving to HEAD~2
f695338 HEAD@{3}: reset: moving to ORIG_HEAD
$ git reset --hard HEAD@{1}
HEAD is now at f695338 Revert "add b"

One command brought back both “discarded” commits, add d and the revert. The edit to a.txt did not come back: it had never been committed, so Git had never stored it.

How long the net lasts

The git-gc manual sets the defaults. Reflog entries expire after 90 days, and entries no longer reachable from the current branch tip, which is exactly what a hard reset creates, expire after 30 days. A commit thrown away by mistake has about a month before garbage collection can remove it for good.

Net

Reverting a merge, and the trap that follows

Revert needs to know which side won

A third test repository had a feature branch with two commits, feature 1 and feature 2, merged into main after a separate commit on main. Asking Git to revert the merge commit plainly failed:

$ git revert --no-edit HEAD
error: commit 93d6e2b... is a merge but no -m option was given.
fatal: revert failed

The git-revert manual explains why: a merge has two parents, and Git cannot know which side should count as the mainline. -m 1 names the first parent, the branch that was merged into:

$ git revert --no-edit -m 1 HEAD
[main 3b536c2] Revert "Merge branch 'feature'"
$ ls
base.txt  m.txt

Both feature files were gone, and main kept its own work.

Merging the fixed branch again

Then the feature branch got a fix, feature 3 fix, which added f3.txt, and was merged into main a second time. The result surprised nobody who had read the manual and would surprise most people who had not:

$ ls
base.txt  f3.txt  m.txt

Only the fix arrived. f1.txt and f2.txt stayed missing. The same manual warns that reverting a merge declares the merged changes unwanted forever, so later merges bring in only commits that were not part of the reverted merge. The original feature commits were already in main‘s history, so Git considered them merged, then undone, and did not merge them again.

The fix is to revert the revert. Running git revert on the “Revert Merge” commit produced a commit Git titled Reapply "Merge branch 'feature'", and all three feature files appeared. Anyone who reverts a merge to buy time should write down that the revert itself will need reverting later.

Choosing the right one

A short decision list

  • A file has edits that should be thrown away, or unstaged: git restore, with --staged for the index.
  • An old version of one file is needed, without moving the branch: git restore --source=<commit> <file>.
  • A commit on a shared branch was a mistake: git revert <commit>.
  • The last few local commits should be redone or squashed: git reset --soft or --mixed.
  • The branch should match another commit exactly and every local change can go: git reset --hard, after a git stash or git status check.

The pushed-commit rule

Resetting a branch that others have already pulled rewrites history they depend on. Pushing it afterward requires a force push, and their next pull will not line up. Revert costs one extra commit in the log. Reset on shared history can cost a team an afternoon.

The uncommitted-work rule

Before any --hard, a quick git stash turns uncommitted edits into a commit-like object the reflog can find later. It takes two seconds. The test file’s unsaved work line would still exist if it had been stashed.

Git forgives almost everything that has been committed. The one line in this test that stayed lost was the one nobody saved.

Sources

  • git(1), “Reset, restore and revert”, Git 2.43.0 documentation: https://git-scm.com/docs/git/2.43.0
  • git-restore, Git 2.43.0 documentation: https://git-scm.com/docs/git-restore/2.43.0
  • git-reset, Git documentation (2.39.3, unchanged through 2.43): https://git-scm.com/docs/git-reset/2.39.3
  • git-gc, Git 2.43.0 documentation: https://git-scm.com/docs/git-gc/2.43.0
  • git-revert, Git 2.43.0 documentation: https://git-scm.com/docs/git-revert/2.43.0

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *