Episode 7 of 12
Undoing Things
Learn how to undo changes, unstage files, and revert commits in Git.
Mistakes happen! Git provides several ways to undo changes at different stages.
Undo Changes in Working Directory
# Discard changes to a specific file (revert to last commit)
git checkout -- filename.txt
# Modern syntax
git restore filename.txt
# Discard ALL uncommitted changes
git checkout -- .
git restore .
Unstage Files
# Remove from staging area (keep changes)
git reset HEAD filename.txt
# Modern syntax
git restore --staged filename.txt
Amend the Last Commit
# Fix the last commit message
git commit --amend -m "New corrected message"
# Add forgotten files to the last commit
git add forgotten-file.txt
git commit --amend --no-edit
Revert a Commit
# Create a new commit that undoes a previous commit (safe)
git revert abc1234
This is the safe way to undo — it creates a new commit rather than erasing history.
Reset to a Previous Commit
# Soft reset — uncommit but keep changes staged
git reset --soft HEAD~1
# Mixed reset (default) — uncommit and unstage
git reset HEAD~1
# Hard reset — uncommit and DELETE all changes (dangerous!)
git reset --hard HEAD~1
Important Warning
git revert— safe, creates a new undo commitgit reset --soft— safe, keeps your changesgit reset --hard— DANGEROUS, permanently deletes changes
Use git revert for shared branches. Use git reset only for local, unpushed commits.