Skip to main content

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, and git 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

Switch to a different branch
git checkout feature-branch

Restoring Files from a Commit

Restore a file from the last commit
git checkout HEAD -- filename.txt
Restore a file from a specific commit
git checkout abc123 -- filename.txt
tip

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:

Undo last commit but keep changes staged
git reset --soft HEAD~1

Mixed Reset (Default)

A mixed reset moves the branch pointer and unstages changes:

Undo last commit and unstage 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:

Completely undo last commit and all changes
git reset --hard HEAD~1
warning

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

Create a new commit that undoes a specific commit
git revert abc123

Reverting Multiple Commits

Revert a range of 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

Unstage all files
git reset
Unstage a specific file
git reset filename.txt

Scenario 2: You made a bad commit locally

Undo the commit but keep changes for fixing
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

Safely undo published changes
git revert bad-commit-hash
git push origin main

Choosing the Right Tool

Use when changes haven't been shared with others:

  • git reset: When you want to rewrite local history
  • git checkout: When you want to restore files or switch branches
  • git revert: Not typically needed for local-only changes

Common Pitfalls

  • Using reset --hard on shared branches: This rewrites history and can cause issues for other developers who have pulled your changes
  • Forgetting that revert creates a new commit: Some developers expect it to remove commits entirely
  • Confusing checkout with reset: Remember that checkout moves HEAD to a different commit/branch, while reset moves the current branch pointer
  • Not checking git status first: 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 commits
  • git 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?

Question 1/5