TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 11 of 12 · By Ethan Cole · Updated November 21, 2026

Undo Changes in Git: restore, reset and revert

Everyone makes mistakes, and Git has an undo for nearly all of them. The trick is choosing the command that matches where the mistake is: in an unsaved edit, in the staging area, or in a commit.

Discard an edit: git restore

git restore notes.txt throws away uncommitted changes to the file and brings back the version from the last commit (strictly, from the staging area). This is one of the few Git commands that can destroy work, since the discarded edit was never saved anywhere. Double-check with git diff first.

Unstage: git restore --staged

git restore --staged notes.txt takes a file out of the staging area but keeps your edits in the working directory. Use it when you ran git add . and caught something that should not be in this commit.

Fix the last commit: --amend

git commit --amend -m "Better message" replaces the most recent commit with a new one that includes anything you have staged and the new message. It is perfect for a typo or a forgotten file, before you have shared the commit.

Move a branch back: git reset

git reset moves the current branch to an earlier commit:

  • git reset --soft HEAD~1 undoes the last commit but keeps its changes staged.
  • git reset HEAD~1 (the default, --mixed) keeps the changes in your files, unstaged.
  • git reset --hard HEAD~1 discards the commit and the changes. Use with great care.

Because reset rewrites history, use it only on commits that exist nowhere else.

Undo a shared commit: git revert

git revert <commit> creates a new commit that does the opposite of an old one. History is not rewritten, so it is safe on branches other people have pulled. This is the right way to back out a change that is already on a shared main.

Terminal · bash + git · copy and run locally
git init -q
echo "Important notes" > notes.txt
git add notes.txt
git commit -q -m "Add notes"
echo "oops, overwritten" > notes.txt
git restore notes.txt
cat notes.txt
echo "draft" > draft.txt
git add draft.txt
git status --short
git restore --staged draft.txt
git status --short
echo "Bad idea" >> notes.txt
git commit -q -am "Add bad idea"
git revert --no-edit HEAD
cat notes.txt
git log --oneline --format="%s"

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q
$ echo "Important notes" > notes.txt
$ git add notes.txt
$ git commit -q -m "Add notes"
$ echo "oops, overwritten" > notes.txt
$ git restore notes.txt
$ cat notes.txt
Important notes
$ echo "draft" > draft.txt
$ git add draft.txt
$ git status --short
A  draft.txt
$ git restore --staged draft.txt
$ git status --short
?? draft.txt
$ echo "Bad idea" >> notes.txt
$ git commit -q -am "Add bad idea"
$ git revert --no-edit HEAD
[main 74580dd] Revert "Add bad idea"
 Date: Thu Jan 15 10:00:00 2026 +0000
 1 file changed, 1 deletion(-)
$ cat notes.txt
Important notes
$ git log --oneline --format="%s"
Revert "Add bad idea"
Add bad idea
Add notes

The transcript damages a file and restores it, unstages a file, then reverts a commit and shows that history grew by one commit instead of shrinking.

A quick decision guide

Situation Command
Edited a file, want the committed version back git restore file
Staged something by accident git restore --staged file
Last commit has a typo, not pushed git commit --amend
Last commit was a mistake, not pushed git reset --soft HEAD~1
A pushed commit must be undone git revert <hash>

Summary

  • restore for files, restore --staged for the staging area.
  • reset and --amend rewrite local history; keep them for unshared commits.
  • revert adds an undo commit and is safe on shared branches.

Quiz: Undoing changes

1. A bad commit is already on the shared main branch. What is the safe way to undo it?

revert adds a new commit that reverses the change without rewriting shared history.

2. You staged secrets.txt by mistake. How do you unstage it but keep the file?

--staged only touches the staging area.

3. Which reset mode keeps the undone commit's changes staged?

--soft moves the branch but leaves the staging area and files as they were.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.