Back to Curriculum

Undoing Changes

📚 Lesson 5 of 12 ⏱️ 35 min

Undoing Changes

35 min

`git commit --amend` allows you to change your last commit. This is useful for fixing typos in commit messages or adding forgotten files to the last commit. Amending creates a new commit that replaces the old one, so it changes commit history. Never amend commits that have been pushed to shared branches—it rewrites history that others may have based work on. Understanding when it's safe to amend is crucial.

`git reset <file>` unstages a file, but it preserves the file contents. This removes the file from the staging area but keeps your changes in the working directory. It's useful when you've accidentally staged a file or want to unstage some changes while keeping others staged. The file remains modified in your working directory, so you can review and stage it again later if needed.

`git checkout -- <file>` discards changes in the working directory. This permanently discards uncommitted changes to a file, reverting it to the last committed state. Use this carefully—the changes cannot be recovered. For a safer approach, consider stashing changes instead. Understanding the difference between discarding and stashing helps you avoid losing work.

Understanding how to undo changes is crucial for managing your project history safely. Git provides multiple ways to undo changes at different levels: working directory, staging area, and committed changes. Each method has different implications for history and collaboration. Knowing which tool to use in each situation prevents mistakes and data loss.

`git revert` creates a new commit that undoes the changes from a previous commit. Unlike reset, revert doesn't rewrite history—it creates a new commit that reverses the effects of a previous commit. This is safe for shared branches because it doesn't change existing history. Revert is the preferred way to undo changes that have been pushed to shared repositories.

`git reset` has three modes: `--soft` (keeps changes staged), `--mixed` (default, keeps changes in working directory), and `--hard` (discards all changes). Reset moves the branch pointer backward, effectively 'undoing' commits. Use `--hard` with extreme caution—it permanently discards changes. Understanding reset modes helps you undo commits safely without losing work.

Key Concepts

  • git commit --amend modifies the last commit.
  • git reset unstages files or moves branch pointer backward.
  • git checkout -- discards working directory changes.
  • git revert creates a new commit that undoes previous changes.
  • Different undo methods affect different areas (working directory, staging, history).

Learning Objectives

Master

  • Amending the last commit
  • Unstaging files with git reset
  • Discarding working directory changes
  • Reverting commits safely

Develop

  • Understanding Git's undo mechanisms
  • Knowing when to use each undo method
  • Safely managing project history

Tips

  • Use git commit --amend for fixing the last commit before pushing.
  • Use git reset HEAD <file> to unstage without losing changes.
  • Use git revert for undoing commits that have been pushed.
  • Use git stash to temporarily save changes instead of discarding them.

Common Pitfalls

  • Amending commits that have been pushed, rewriting shared history.
  • Using git reset --hard without checking what will be lost.
  • Confusing git reset with git revert, using the wrong tool.
  • Not understanding the difference between reset modes.

Summary

  • Git provides multiple ways to undo changes at different levels.
  • git commit --amend modifies the last commit.
  • git revert safely undoes commits without rewriting history.
  • Understanding undo methods prevents mistakes and data loss.

Exercise

Simulate amending the last commit message.

# Make some changes and commit them
git commit -m "Initial message"

# Realize you made a typo, amend the commit
git commit --amend -m "Corrected message"

Code Editor

Output