Reset and Revert

Master Git reset and revert operations for effective history management

Learning Objectives

  • Understand the differences between reset and revert operations
  • Master the three types of reset: soft, mixed, and hard
  • Learn when and how to use revert for safer history modification
  • Implement effective recovery strategies for common scenarios

Understanding Reset vs Revert

Git provides two main ways to undo changes: reset and revert. While both help you handle unwanted changes, they work very differently and are used in different scenarios.

Git Reset

  • Moves the branch pointer to a different commit
  • Can modify working directory and staging area
  • Best for local changes not yet pushed

Git Revert

  • Creates new commit that undoes previous changes
  • Preserves history and is safe for shared branches
  • Best for published changes that need to be undone

Types of Reset

Git reset comes in three flavors, each affecting different areas of Git's state management:

Soft Reset

Only moves HEAD pointer, leaving staging area and working directory unchanged.

$ git reset --soft HEAD~1

Mixed Reset

Updates HEAD and staging area, leaves working directory unchanged.

$ git reset HEAD~1

Hard Reset

Updates all areas, discarding all changes since the target commit.

$ git reset --hard HEAD~1

Using Git Revert

Git revert is the safer option for undoing changes, especially in shared repositories. Let's look at how to use it effectively:

Basic Revert Operations

# Revert the most recent commit
$ git revert HEAD
# Revert a specific commit
$ git revert abc123
# Revert multiple commits
$ git revert HEAD~2..HEAD
# Revert a merge commit
$ git revert -m 1 merge_commit_hash

Important Note

When reverting merge commits, you must specify which parent to revert to using the -m flag. Usually, -m 1 reverts to the main branch's parent.

Recovery Strategies

Having a good recovery strategy is crucial when working with reset and revert. Here are some common scenarios and their solutions:

Recovering from Hard Reset

# Find the lost commit hash
$ git reflog
# Recover the changes
$ git reset --hard <commit_hash>

Fixing a Bad Revert

# Revert the revert
$ git revert revert_commit_hash
# Or reset if not pushed
$ git reset --hard HEAD~1

Recovering Staged Changes

# Restore files from index
$ git reset --hard
$ git clean -fd # Remove untracked files

Best Practices

  • Always use revert for changes that have been pushed to remote repositories
  • Use reset primarily for local changes and cleanup
  • Always check git reflog before performing destructive operations
  • Consider creating backup branches before major history modifications

What's Next?

Now that you understand how to modify history safely with reset and revert, you're ready to dive deeper into Git's internals. In the next lesson, you'll learn about:

  • Git's object model and how it stores data
  • Understanding refs and refs of refs
  • How Git manages and optimizes storage with packfiles