Reset, Revert, and Restore

Reset, Revert, and Restore

Git Reset Revert

Introduction

These commands help you undo changes at different levels. Understanding each is essential for working with Git.

git restore

Restores files in the working directory. New in Git 2.23+.

Restore Working Directory File

git restore filename.txt

Discards changes in working directory.

Unstage a File

git restore --staged filename.txt

Or the old way:

git reset HEAD filename.txt

Restore to Specific Version

git restore --source=HEAD~2 filename.txt
git restore --source=v1.0.0 filename.txt

git reset

Moves the branch pointer and optionally modifies staging area and working directory.

Three Modes

Soft Reset

git reset --soft HEAD~1

Moves branch back one commit:

  • Working directory: unchanged
  • Staging area: unchanged
  • Branch pointer: moved back
Changes are ready to commit again.

Mixed Reset (Default)

git reset HEAD~1
git reset --mixed HEAD~1
  • Working directory: unchanged
  • Staging area: reset to match
  • Branch pointer: moved back
Changes are in working directory, not staged.

Hard Reset

git reset --hard HEAD~1
  • Working directory: reset to match
  • Staging area: reset to match
  • Branch pointer: moved back
⚠️ This PERMANENTLY deletes changes!

Reset Examples

Unstage Everything

git reset

Unstage Specific File

git reset file.txt

Go Back 3 Commits (Keep Changes)

git reset --soft HEAD~3

Go Back 3 Commits (Discard Changes)

git reset --hard HEAD~3

git revert

Creates a new commit that undoes previous changes.

Basic Revert

git revert HEAD

Creates a new commit that undoes the last commit.

Revert Specific Commit

git revert abc1234

Creates a commit undoing that specific commit.

Revert Without Commit

git revert -n HEAD
git revert -n abc1234

Stages changes but doesn't create commit. Useful for reverting multiple:

git revert -n HEAD~3..HEAD
git commit -m "Revert last 3 commits"

Comparing the Commands

git restore

  • Only affects working directory or staging
  • No commit history changes
  • For uncommitted changes

git reset

  • Moves branch pointer
  • Can modify staging and working directory
  • Rewrites history

git revert

  • Creates new commit
  • Safe for shared branches
  • Preserves history

When to Use Each

Use git restore when:

  • Want to discard uncommitted changes
  • Need to unstage a file
  • Restoring specific file versions

Use git reset when:

  • Undoing local commits
  • Cleaning up before push
  • Moving branch to different commit

Use git revert when:

  • Undoing already pushed commits
  • Working on shared branches
  • Need audit trail of undo

Practical Examples

Example 1: Undo Uncommitted Changes

# Discard changes in file
git restore filename.txt

Discard all changes

git restore .

Example 2: Undo Last Commit (Local)

# Keep changes staged
git reset --soft HEAD~1

Don't keep changes

git reset --hard HEAD~1

Example 3: Undo Pushed Commit

# Create undo commit
git revert abc1234

Push the revert

git push origin main

Example 4: Undo Multiple Commits

# Revert last 5 commits
git revert HEAD~4..HEAD

Or individually

git revert HEAD~4 git revert HEAD~3 git revert HEAD~2

Recovering from Mistakes

Reflog

Git keeps a reflog of all HEAD changes:

git reflog

Output:

abc1234 HEAD@{0}: commit: Add feature
def5678 HEAD@{1}: checkout: moving to main
ghi9012 HEAD@{2}: commit: Previous work

Recover Lost Commit

git reset --hard HEAD@{1}

Recover Deleted Branch

git reflog
git checkout -b recovered-branch abc1234

Summary

CommandUse Case
git restore fileDiscard uncommitted changes
git reset --softUndo commit, keep changes staged
git reset --mixedUndo commit, keep changes unstaged
git reset --hardPermanently discard changes
git revertUndo already pushed commits safely
Choose the right tool for your situation.

Next Lesson

Learn about stashing - saving work in progress.

Quiz - Quiz - Reset, Revert, and Restore

1. Which command restores a file from the last commit without affecting history?

2. What does 'git reset --hard HEAD~1' do?

3. What does 'git revert' create?

4. Which command is safe to use on already pushed commits?

5. What does 'git reflog' show?

Rebase vs Merge