Undoing Changes - Reset Revert Checkout
Lesson 14: Undoing Changes: Reset, Revert, Checkout
Sometimes in Git, we need to go back in time. Whether you've made a mistake in a commit, staged the wrong files, or just want to explore an earlier version of your code, Git provides several powerful tools for undoing changes. In this lesson, you'll learn when and how to use reset, revert, and checkout to fix mistakes and navigate your project's history safely.
Learning Goals
- Understand the differences between
git reset,git revert, andgit checkout - Know when to use each command based on your specific situation
- Learn how to undo changes at different stages (working directory, staging area, committed changes)
- Practice safe undoing techniques that preserve your work and collaboration history
Understanding the Three States
Before we dive into the commands, let's review Git's three main areas where changes can exist:
- Working Directory: Your actual files on disk
- Staging Area: Files you've
git added but not committed - Repository: Committed changes in your Git history
Each undoing command affects these areas differently.
git checkout: Navigating and Restoring Files
git checkout is primarily used for switching branches or restoring files from specific commits.
Switching Branches
git checkout feature-branch
Restoring Files from a Commit
git checkout HEAD -- filename.txt
git checkout abc123 -- filename.txt
Use git checkout -- <file> to discard changes in your working directory and restore the file to its last committed state. This is safe and only affects uncommitted changes.
git reset: Rewriting History (Carefully!)
git reset moves the current branch pointer to a different commit, and optionally updates your working directory and staging area.
Soft Reset
A soft reset moves the branch pointer but keeps your changes staged:
git reset --soft HEAD~1
Mixed Reset (Default)
A mixed reset moves the branch pointer and unstages changes:
git reset HEAD~1
# or explicitly
git reset --mixed HEAD~1
Hard Reset
A hard reset moves the branch pointer, unstages changes, AND discards working directory changes:
git reset --hard HEAD~1
git reset --hard is destructive! It permanently discards uncommitted changes. Always make sure you don't need any of your current work before using it.
git revert: Safe Undo for Published Changes
git revert creates a new commit that undoes the changes from a previous commit. This is the safe option when you've already pushed changes to a shared repository.
Reverting a Single Commit
git revert abc123
Reverting Multiple Commits
git revert HEAD~3..HEAD
Practical Examples
Let's walk through some common scenarios where you might need to undo changes.
Scenario 1: You staged the wrong files
git reset
git reset filename.txt
Scenario 2: You made a bad commit locally
git reset --soft HEAD~1
# Fix your changes, then recommit
git commit -m "Fixed previous mistake"
Scenario 3: You pushed a bad commit to shared repository
git revert bad-commit-hash
git push origin main
Choosing the Right Tool
- Local Repository Only
- Shared Repository
Use when changes haven't been shared with others:
git reset: When you want to rewrite local historygit checkout: When you want to restore files or switch branchesgit revert: Not typically needed for local-only changes
Use when changes have been pushed to a shared repository:
git revert: Always use this for undoing published commitsgit checkout: Safe for restoring filesgit reset: Avoid on shared branches (can cause conflicts for others)
Common Pitfalls
- Using
reset --hardon shared branches: This rewrites history and can cause issues for other developers who have pulled your changes - Forgetting that
revertcreates a new commit: Some developers expect it to remove commits entirely - Confusing
checkoutwithreset: Remember thatcheckoutmoves HEAD to a different commit/branch, whileresetmoves the current branch pointer - Not checking
git statusfirst: Always verify what you're about to undo - Undoing without backup: Consider creating a temporary branch before major undo operations
Summary
git checkout: Use for switching branches and restoring files from specific commitsgit reset: Use for undoing local commits (be careful with--hard)git revert: Use for undoing commits that have been shared with others
The golden rule: use revert for shared history and reset for local history. Always think about whether others might have your changes before choosing an undo strategy.
Quiz
Git Cleanup and Recovery – Quick Check
You accidentally committed a file with sensitive information that hasn't been pushed yet. Which command should you use?