Lesson 12: Undoing Changes
What You'll Learn
In this lesson, you'll learn how to undo changes in Git. Whether you made a mistake, want to discard changes, or need to revert a commit, Git has tools to help you fix things safely.
Types of Undo Operations
| Situation | Solution |
|---|---|
| Made changes but haven't staged them | git restore filename |
| Staged changes but haven't committed | git restore --staged filename |
| Committed but want to modify the commit | git commit --amend |
| Need to undo commits (keep changes) | git reset --soft |
| Need to undo commits (discard changes) | git reset --hard |
| Committed bad code (already pushed) | git revert |
Part 1: Undoing Unstaged Changes
Discard Changes in a File
If you modified a file but haven't staged it yet:
git restore index.html
Warning: This permanently discards your changes! The file returns to how it looked in the last commit.
Discard All Unstaged Changes
git restore .
Old Command (Still Works)
git checkout -- index.html
Part 2: Unstaging Files
Remove File from Staging Area
If you staged a file but want to unstage it:
git restore --staged index.html
The changes stay in the file, but it's removed from the staging area.
Unstage All Files
git restore --staged .
Old Command (Still Works)
git reset HEAD index.html
Part 3: Amending the Last Commit
Fix the Last Commit Message
git commit --amend -m "New commit message"
Add Forgotten Files to Last Commit
# Oh no! Forgot to add a file
git add forgotten-file.txt
git commit --amend --no-edit
The --no-edit flag keeps the same commit message.
Example scenario:
# Made commit
git commit -m "Add login feature"
# Oops! Forgot style.css
git add style.css
git commit --amend --no-edit
# Now the commit includes style.css!
Warning: Only amend commits that haven't been pushed! Amending changes the commit hash.
Part 4: git reset
What is git reset?
Moves the branch pointer to a different commit, potentially changing your staging area and working directory.
Three Types of Reset
| Type | Working Directory | Staging Area | Use Case |
|---|---|---|---|
--soft |
Unchanged | Unchanged | Undo commit, keep changes staged |
--mixed (default) |
Unchanged | Reset | Undo commit and unstage |
--hard |
Reset | Reset | Completely undo (DANGEROUS!) |
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
Commits are undone, but changes stay staged.
Undo Last Commit (Unstage Changes)
git reset HEAD~1
Commits are undone, changes are unstaged but still in files.
Completely Undo Last Commit
git reset --hard HEAD~1
⚠️ DANGER: This permanently deletes your changes!
Reset to Specific Commit
git reset --hard abc1234
Part 5: git revert
What is git revert?
Creates a new commit that undoes a previous commit. Unlike reset, it's safe to use on public branches.
When to Use Revert
- After pushing: Already pushed the bad commit
- Team collaboration: Others may have pulled your changes
- Keep history: Want to preserve the record of the mistake
Revert the Last Commit
git revert HEAD
Git opens an editor with a commit message like:
Revert "Add broken feature"
This reverts commit abc1234.
Revert Specific Commit
git revert abc1234
Revert Without Opening Editor
git revert HEAD --no-edit
Part 6: Reset vs Revert
| Aspect | git reset | git revert |
|---|---|---|
| Changes history? | Yes - rewrites history | No - adds new commit |
| Safe for public branches? | No - causes problems | Yes - safe to push |
| Use when | Changes not yet pushed | Changes already pushed |
| Result | Commits disappear | New "undo" commit created |
Rule of thumb:
- Not pushed yet? Use
git reset - Already pushed? Use
git revert
Part 7: Recovering Lost Commits
git reflog
If you accidentally reset too far, reflog can save you!
git reflog
Output:
abc1234 HEAD@{0}: reset: moving to HEAD~1
def5678 HEAD@{1}: commit: Add feature
ghi9012 HEAD@{2}: commit: Fix bug
To recover a lost commit:
git reset --hard def5678
The reflog is a lifesaver! It tracks every HEAD movement for 90 days.
Part 8: Other Useful Undo Commands
Temporarily Save Changes (Stash)
# Save current changes
git stash
# Do something else...
# Restore changes
git stash pop
Clean Untracked Files
# See what would be removed
git clean -n
# Actually remove untracked files
git clean -f
# Also remove untracked directories
git clean -fd
Practice Exercise
- Make a change to a file and discard it:
echo "Test" >> test.txt git restore test.txt - Stage a file, then unstage it:
echo "Test" >> test.txt git add test.txt git restore --staged test.txt - Make a commit with a typo, then fix it:
git commit -m "Add feture" # Oops, typo! git commit --amend -m "Add feature" - View your reflog:
git reflog
Summary
Quick Reference
| To Undo... | Use This |
|---|---|
| Unstaged changes | git restore file |
| Staged changes | git restore --staged file |
| Last commit message | git commit --amend |
| Unpushed commits | git reset |
| Pushed commits | git revert |
| Find lost commits | git reflog |
Remember: Be careful with --hard flags - they permanently delete changes!
What's Next?
In the next lesson, you'll learn how to collaborate with others - forking repositories, contributing to open source, and working effectively in teams.