Undoing Changes — Module Recap
Module 04 is complete. You now have the full toolkit for undoing changes at every stage: working tree, staging area, the most recent commit, any previous commit, and ranges of commits.
What you learned
Section titled “What you learned”Discarding working-tree changes — git restore <file> overwrites the file with the staged or last-committed version. Destructive — changes not recoverable from Git. Review git diff before discarding.
Unstaging files — git restore --staged <file> removes a file from the staging area without touching the working tree. Not destructive. Use it to split staged changes into separate commits.
Amending the last commit — git commit --amend replaces the last commit with a new one — with a fixed message, additional staged content, or both. Creates a new hash. Only safe for unpushed commits.
git revert — Creates a new commit that is the inverse of a target commit. Preserves full history. Safe for pushed commits. The right choice when you need to undo something on a shared branch.
git reset — Moves the branch pointer backward, removing commits from the tip of the branch. Three modes: --soft (keep staged), --mixed (keep unstaged), --hard (discard). Only safe for unpushed commits. git reflog is the recovery tool if you reset too far.
The decision matrix
Section titled “The decision matrix”| Situation | Right tool |
|---|---|
| Discard unsaved working-tree changes | git restore <file> |
| Remove a file from staging | git restore --staged <file> |
| Fix the last commit’s message (not pushed) | git commit --amend -m "..." |
| Add a forgotten file to the last commit (not pushed) | git add <file> then git commit --amend --no-edit |
| Undo a commit on a pushed/shared branch | git revert <hash> |
| Undo a local-only commit, keep changes staged | git reset --soft HEAD~1 |
| Undo a local-only commit, keep changes unstaged | git reset HEAD~1 |
| Undo a local-only commit and discard all changes | git reset --hard HEAD~1 |
| Recover from an accidental hard reset | git reflog then git reset --hard <recovered-hash> |
Command reference
Section titled “Command reference”| Command | What it does |
|---|---|
git restore <file> | Discard working-tree changes (destructive) |
git restore --staged <file> | Remove from staging area (keep working-tree changes) |
git restore --staged . | Unstage everything |
git commit --amend | Replace last commit (opens editor) |
git commit --amend -m "msg" | Replace last commit with new message |
git commit --amend --no-edit | Replace last commit, keep message |
git revert <hash> | Create a new commit inverting the target commit |
git revert HEAD | Revert the most recent commit |
git revert --no-edit | Skip editor for revert commit message |
git reset --soft HEAD~n | Move branch back n commits, keep changes staged |
git reset HEAD~n | Move branch back n commits, keep changes unstaged |
git reset --hard HEAD~n | Move branch back n commits, discard all changes |
git reflog | Log of all HEAD movements — recovery tool |
What’s next — Working with GitHub
Section titled “What’s next — Working with GitHub”Module 05 takes Git from your local machine to the internet. You will connect a local repository to GitHub, push and pull changes, and understand the relationship between local branches and their remote counterparts.
Everything you have learned about commits, branches, and history is the same on GitHub — you are just sharing it with the world.