Undo, beginner edition
Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI
Why this matters
Mistakes are the normal texture of Git work: an edit in the wrong file, a file staged by accident, a commit with a typo, a change that turned out wrong. Each stage has one clean undo. Learn those four, and the fear goes away; leave the powerful ones for Section 12, when you have branches and remotes to recover from.
The map
| The mistake is in… | Undo with | Loses work? |
|---|---|---|
| the working tree (an edit you do not want) | git restore <file> |
Yes: the uncommitted edit is gone |
| the staging area (staged the wrong file) | git restore --staged <file> |
No: the edit stays in the working tree |
| the last commit (message or a forgotten file), not pushed | git commit --amend |
No |
| a commit that was pushed or is older than the last | git revert <hash> |
No: a new commit undoes it |
Everything else (reset --hard, push --force, rebase, checkout of old commits) belongs to Section 12.
1. Discard an edit
You changed a file and want the committed version back:
$ git status -s M README.md$ git restore README.md
$ git status -sSilence: the file matches the last commit again (or the staged version, if one exists). This is the one everyday command that throws work away without a way back, so look at git diff <file> first and be sure.
2. Unstage
You staged a file that should not be in the next commit:
$ git status -sM docs/faq.md$ git restore --staged docs/faq.md
$ git status -s M docs/faq.mdThe M moved from the first column to the second: no longer staged, still edited. Nothing lost. To also drop the edit, follow with plain git restore docs/faq.md.
3. Fix the last commit (before pushing)
Forgot a file:
$ git add docs/faq.md
$ git commit --amend --no-edit[main 8330ba9] feat: add Coast Walk trail (#29)
Date: Tue Sep 8 10:00:00 2026 +0200
2 files changed, 5 insertions(+)Wrong message:
$ git commit --amend -m "feat: add Coast Walk trail and FAQ entry (#29)"[main 778c9e7] feat: add Coast Walk trail and FAQ entry (#29)
Date: Tue Sep 8 10:00:00 2026 +0200
2 files changed, 5 insertions(+)Each amend produces a new hash; the previous commit is replaced. This rewrites history, so: only if you have not pushed (lesson 2.7). After a push, use a new commit.
4. Undo a commit with a new commit
A change is wrong, and it is already pushed, or it is not the last one. git revert creates a new commit that applies the opposite change; history keeps both:
$ git revert --no-edit HEAD[main dc9a369] Revert "feat: add Coast Walk trail and FAQ entry (#29)"
Date: Tue Sep 8 10:00:00 2026 +0200
2 files changed, 5 deletions(-)$ git log --oneline -3dc9a369 Revert "feat: add Coast Walk trail and FAQ entry (#29)"
778c9e7 feat: add Coast Walk trail and FAQ entry (#29)
c541c80 docs: add maintainer noteThe files are as they were before 778c9e7, and the record shows what happened and why. git revert <hash> works on any commit, not just the last; without --no-edit it opens the editor so you can add a reason to the message. Because it only adds a commit, it is always safe to push.
A bonus: one file from an older commit
Not an undo of a commit, but a frequent need — bring back a file's content from an earlier point, into the working tree, without touching anything else:
$ git restore --source=HEAD~3 README.md
$ git status -s M README.mdThe file is now as it was three commits ago, as an uncommitted change you can inspect, commit, or discard.
In the tools
Everything above. Before any git restore without --staged, run git diff <file> and read what you are about to lose.
- Discard an edit: in Source Control, hover the file under Changes → the ↶ Discard Changes icon. VS Code asks for confirmation because it is irreversible (deleted untracked files go to the system trash, not into the void).
- Unstage: the − icon on a file under Staged Changes.
- Amend: the … menu → Commit (Amend), with the corrected message in the box and any extra files staged.
- Revert: in the Source Control Graph, right-click a commit → Revert Commit (VS Code 1.9x and later); or use the terminal.
- Discard an edit: right-click the file in the Commit window → Rollback (Ctrl+Alt+Z / ⌥⌘Z). IntelliJ asks for confirmation. Its Local History (right-click → Local History → Show History) keeps a private copy of recent edits even after a rollback — a safety net the terminal does not have.
- Unstage (with the staging area enabled): the − on the file; with changelists, simply untick the file before committing.
- Amend: tick Amend in the commit panel.
- Revert: in the Log tab, right-click a commit → Revert Commit.
Uncommitted work exists only on your computer, so undos 1–3 have no web equivalent. For undo 4: open the commit (Code → Commits), then Options → Revert. GitLab creates the revert commit on a new branch and opens a merge request, or commits directly if the branch allows it. A merge request page has the same Revert option after merging.
Same: on a commit page, the … menu offers no revert, but a merged pull request has a Revert button that opens a new pull request with the inverse change. For a single commit, use the terminal.
Common mistakes
git restorewhengit restore --stagedwas meant. The edit is gone. Read the hint lines ingit status; they always say which form.- Amending a pushed commit. Diverged branch. Revert or a new commit instead.
- Reverting a merge commit without
-m. Git refuses and explains; that case is in lesson 12.6. - Reaching for
git reset --hardfrom a web search. It discards everything uncommitted and moves the branch. Not yet.
Try it yourself
Goal: perform all four undos in the playground.
- Edit
README.md, checkgit diff, then discard it withgit restore README.md. - Edit
docs/faq.md, stage it, then unstage it withgit restore --staged docs/faq.md; confirm the edit is still there withgit diff, then discard it. - Make a commit with a wrong message (
git commit --allow-empty -m "docs: typoo"), then fix it withgit commit --amend -m "docs: typo". - Make a real commit (add a line to the FAQ, stage, commit), then
git revert --no-edit HEADand readgit log --oneline -3.
Expected result: the tree is clean after 1 and 2; the log shows the corrected message after 3; after 4 the log shows the commit and its revert, and the FAQ no longer has the line.
Show solution
Undo 1 and 2 are working-tree and staging-area operations that leave no trace in history. Undo 3 replaced the commit (new hash). Undo 4 added a commit: git log shows both, and git show HEAD shows the four lines being removed again. That is the complete beginner's toolkit.