DEV Community

ShellProven
ShellProven

Posted on Originally published at shellprovenai.github.io

Undo anything in Git: restore vs reset vs revert (live demos)

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (git 2.47.3); all outputs below are copied from that run, not written by hand.

Five real "undo" situations in Git, each shown live: throw away an edit with git restore, unstage a file with git restore --staged, undo the last commit but keep the changes with git reset --soft, drop a commit with git reset --hard and get it back with git reflog, and safely undo a commit that's already shared with git revert.

The problem: Git has at least three commands that undo things: restore, reset and revert. They sound alike, but they do very different jobs, and picking the wrong one can cost you work.

The demo repository

Our demo repository has one config file and three commits.

$ git log --oneline
f1f7a48 Enable debug
b01e5d3 Add currency
52e0461 Add config
Enter fullscreen mode Exit fullscreen mode

The newest commit is at the top: enable debug. Your commit hashes will look different from mine, that's normal.

1. Throw away an edit: git restore

Situation one. You changed a file, you haven't committed, and you want the change gone.

$ echo "price = 0" >> app.conf
Enter fullscreen mode Exit fullscreen mode

git status --short shows the file as modified.

$ git status --short
 M app.conf
Enter fullscreen mode Exit fullscreen mode

git restore puts the file back to the last committed version.

$ git restore app.conf
Enter fullscreen mode Exit fullscreen mode

The status is clean again, and the file no longer has the bad price.

$ cat app.conf
price = 10
currency = EUR
debug = true
Enter fullscreen mode Exit fullscreen mode

Careful: this change was never committed, so git has no copy of it. Restore throws it away for good.

2. Unstage a file: git restore --staged

Situation two. You ran git add on something you didn't mean to commit yet.

$ echo "tax = 23" >> app.conf && git add app.conf && git status --short
M  app.conf
Enter fullscreen mode Exit fullscreen mode

The M in the first column means the change is staged. git restore --staged takes it out of the staging area, but keeps your edit in the file.

$ git restore --staged app.conf && git status --short
 M app.conf
Enter fullscreen mode Exit fullscreen mode

Now the M moved to the second column: modified, but not staged. Your work is safe. Let me clean it up before we continue.

$ git restore app.conf
Enter fullscreen mode Exit fullscreen mode

3. Undo the last commit, keep the changes: reset --soft

Situation three. You committed too early, or with a bad message, and you want to redo the commit. git reset --soft HEAD~1 moves the branch back by one commit. The changes from that commit stay staged.

$ git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
$ git status --short && git log --oneline
M  app.conf
b01e5d3 Add currency
52e0461 Add config
Enter fullscreen mode Exit fullscreen mode

The enable debug commit is gone from the log, but its change is still staged. Now we can commit it again, with a better message.

$ git commit -qm "Enable debug (only in dev)" && git log --oneline
4949205 Enable debug (only in dev)
b01e5d3 Add currency
52e0461 Add config
Enter fullscreen mode Exit fullscreen mode

4. Drop a commit with reset --hard, then get it back

Situation four. You want the last commit gone completely, changes included. That's reset --hard. This is the dangerous one: it also wipes any uncommitted changes in your working folder. Run git status first and make sure there is nothing you want to keep.

$ git reset --hard HEAD~1
HEAD is now at b01e5d3 Add currency
Enter fullscreen mode Exit fullscreen mode
$ cat app.conf
price = 10
currency = EUR
Enter fullscreen mode Exit fullscreen mode

The debug line is gone from the file and from history. But if you regret it, git still remembers. git reflog lists every place your branch has pointed to.

$ git reflog | head -3
b01e5d3 HEAD@{0}: reset: moving to HEAD~1
4949205 HEAD@{1}: commit: Enable debug (only in dev)
b01e5d3 HEAD@{2}: reset: moving to HEAD~1
Enter fullscreen mode Exit fullscreen mode

The commit we dropped is still there, one step back, at HEAD@{1}. Resetting to it brings it back.

$ git reset --hard "HEAD@{1}" && git log --oneline
HEAD is now at 4949205 Enable debug (only in dev)
4949205 Enable debug (only in dev)
b01e5d3 Add currency
52e0461 Add config
Enter fullscreen mode Exit fullscreen mode

The reflog only lives in your local repository, and old entries expire after a few weeks. It saves you from a mistake today, it's not a backup.

5. Undo a shared commit: git revert

Situation five. The commit is already pushed and your teammates have pulled it. Rewriting history with reset would break their copies. git revert doesn't delete anything. It adds a new commit that does the opposite.

$ git revert --no-edit HEAD
[main e8bee19] Revert "Enable debug (only in dev)"
 Date: Sat Nov 21 19:07:10 2026 +0000
 1 file changed, 1 deletion(-)
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline && cat app.conf
e8bee19 Revert "Enable debug (only in dev)"
4949205 Enable debug (only in dev)
b01e5d3 Add currency
52e0461 Add config
price = 10
currency = EUR
Enter fullscreen mode Exit fullscreen mode

History now shows both the original commit and its revert, and the debug line is gone from the file. Everyone can simply pull this.

The trap, in one paragraph

I ran git reset --hard and the commit vanished. Is it gone for good? Not right away. git reflog still lists it, and you can reset back to it.

Quick check

1. My bad commit is already pushed, and my team pulled it. Do I use reset or revert?

  • git reset --hard HEAD~1
  • git revert HEAD
  • git restore

Answer
git revert HEAD: Revert adds a new commit that undoes the change, so nobody's history breaks. Reset rewrites history.

2. I ran git reset --hard and the commit vanished. Is it gone for good?

  • Yes, reset --hard deletes it at once
  • No: git reflog still lists it, and you can reset back

Answer
No: git reflog still lists it, and you can reset back: Not right away. git reflog still lists it, and you can reset back to it.

Recap

Uncommitted edit: git restore. Staged by mistake: git restore --staged. Redo your last local commit: reset --soft. Drop it completely: reset --hard, and git reflog if you regret it. Already pushed: git revert.

Source and reproducible setup


This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

Top comments (0)