All courses

Undoing with git: discard, reset, revert

Choose the right undo. Discard uncommitted changes to files, return to the last commit, or revert a commit that is already made. Know which of these permanently loses work.

Builds on: git status and reading a diff

Read it card by card Start the whole course

Three things went wrong. Which undo?

An AI coding agent has worked in your project, and you find three kinds of trouble: 1. One file has uncommitted edits you don't want, but the other edits are fine. 2. Every uncommitted edit is bad. 3. A commit you made yesterday turns out to break something. Each case needs a different git command. Two of these commands destroy work for good.

Uncommitted work has no backup

Git only keeps copies of what you committed. The files on disk (the **working tree**) may differ from the last commit, and those differences exist nowhere else. Throwing away uncommitted edits deletes them for good. There is no recycle bin. A commit is a saved snapshot. To undo one, you don't erase it. You add a new commit that does the exact opposite, and both stay in history.

Pick the undo by what you're undoing

- **One file, not committed:** `git restore <file>` puts it back as of the last commit. The edits are lost forever. - **All uncommitted edits:** `git reset --hard` puts every tracked file back to the last commit. The edits are lost forever. - **A commit already made:** `git revert <hash>` adds a new commit that cancels it. Nothing is lost. `HEAD` is git's name for your latest commit, so `git revert HEAD` cancels that one. Run `git status` and `git diff` first.

Discard one file, then everything

First check what you are about to lose. Then discard one file, then all the remaining edits. If you already ran `git add` on a file, use `git restore --staged --worktree <file>` for it. Brand-new untracked files are left alone by both commands. Delete them yourself if you don't want them.
$ git status
  modified:   src/app.py
  modified:   src/utils.py
$ git diff src/utils.py
diff --git a/src/utils.py b/src/utils.py
--- a/src/utils.py
+++ b/src/utils.py
@@ -1,3 +1,3 @@
 import time
 
-TIMEOUT = 30
+TIMEOUT = 0
$ git restore src/utils.py
$ git status
  modified:   src/app.py
$ git reset --hard
HEAD is now at 3f9c2e1 Add login form
$ git status
nothing to commit, working tree clean

Reverting yesterday's commit

`git log --oneline` lists commits newest first. The commit `7d4e8a2 Rewrite date parsing` broke dates, but two good commits came after it. Run `git revert --no-edit 7d4e8a2`. Git adds a new commit that removes exactly those changes, and the later commits stay. `git log --oneline` now shows `Revert "Rewrite date parsing"` on top. Don't `git reset --hard` to an older commit, whether you name it by hash or as `HEAD~1` (the commit just before `HEAD`). Every commit after it is dropped, the good ones too.

Remember

For uncommitted edits, use `git restore <file>` for one file or `git reset --hard` for all of them. Both delete those edits forever, so read `git diff` first. For a commit already made, use `git revert <hash>`. It adds an opposite commit and loses nothing.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.