Pular para o conteúdo principal

Undoing Changes — git reset, git restore, git revert

Module 01 60 min

Section Objectives

  • Understand the different ways to undo changes in Git
  • Use git restore to discard working tree changes
  • Use git reset to undo commits (with varying impact)
  • Use git revert to undo published commits safely
  • Know which command to use in which situation

The 3 Undo Commands — Overview

CommandWhat it undoesSafe for shared branches?
git restoreWorking tree or staging areaYes (local only)
git resetCommits (moves HEAD)Only for local commits
git revertCreates a new "undo" commitYes (safe for teams)

git restore — Discard Local Changes

Discard Changes in Working Tree

# Discard changes to a file (restore to last commit version)
git restore README.md

# Discard all unstaged changes
git restore .

# Restore to a specific commit version
git restore --source=HEAD~2 README.md
Irreversible!

git restore on the working tree is irreversible. Your uncommitted changes will be permanently lost. Use git stash if you want to keep them temporarily.

Unstage Files (Remove from Staging Area)

# Remove a file from staging area (keep changes in working tree)
git restore --staged README.md

# Remove all files from staging area
git restore --staged .

git reset — Undo Commits

git reset moves the HEAD pointer (and the branch) backwards in history. There are 3 modes:

The 3 Modes of git reset

ModeWhat HappensUse Case
--softHEAD moves back, files stay stagedRedo commit message
--mixed (default)HEAD moves back, files go back to working treeRedo what was staged
--hardHEAD moves back, files discardedCompletely undo commits

Examples

# --soft: Undo last commit, keep changes staged
git reset --soft HEAD~1
# → Your files are still staged, ready to recommit

# --mixed (default): Undo last commit, keep changes in working tree
git reset HEAD~1
git reset --mixed HEAD~1
# → Your files are modified but unstaged

# --hard: Completely undo last commit (DANGER: changes lost!)
git reset --hard HEAD~1
# → Working tree restored to state of previous commit

Practical Use Cases

# Undo 3 last commits but keep the files (to squash into one commit)
git reset --soft HEAD~3
git commit -m "feat: complete user authentication system"

# Undo a commit and rework the staging
git reset HEAD~1
git add -p # Stage more carefully
git commit -m "Better commit message and content"

# Discard all work since last commit (DANGER)
git reset --hard HEAD
reset on shared branches

Never use git reset on commits that have already been pushed to a shared branch. This rewrites history and creates major conflicts for your teammates. Use git revert instead.


git revert — Safe Undo for Shared Branches

git revert creates a new commit that undoes the changes of a previous commit. It doesn't rewrite history — it adds to it.

# Revert the last commit
git revert HEAD

# Revert a specific commit
git revert d5a1b4c

# Revert without opening the editor (use auto message)
git revert HEAD --no-edit

# Revert multiple commits
git revert HEAD~3..HEAD

# Revert without committing immediately (stage only)
git revert HEAD --no-commit

When to Use git revert

SituationCommand
Buggy commit on maingit revert <commit>
Undo a deployed featuregit revert <commit>
Shared branch, bad commitgit revert <commit>
Local branch onlygit reset (safer)

Decision Tree — Which Command to Use?


git stash — Temporarily Save Work

git stash lets you temporarily save your work without committing it — useful when you need to switch contexts quickly.

# Save current work (working tree + staging)
git stash

# Save with a descriptive message
git stash save "WIP: login form work in progress"

# List saved stashes
git stash list
# stash@{0}: On main: WIP: login form work in progress
# stash@{1}: WIP on main: d5a1b4c fix: email validation

# Apply last stash (keep it in stash list)
git stash apply

# Apply and remove from stash list
git stash pop

# Apply a specific stash
git stash apply stash@{1}

# Delete a stash
git stash drop stash@{0}

# Delete all stashes
git stash clear

# Show content of a stash
git stash show stash@{0}
git stash show -p stash@{0} # With full diff

Typical Stash Workflow

# You're working on a feature (uncommitted changes)
# Urgency: critical bug to fix on main!

# 1. Save your current work
git stash save "WIP: dashboard feature"

# 2. Switch to main to fix the bug
git checkout main
git pull
# ... fix the bug ...
git commit -m "fix: critical security vulnerability"
git push

# 3. Come back to your feature
git checkout feature/dashboard

# 4. Restore your saved work
git stash pop

Summary Table

CommandDescription
git restore <file>Discard changes in working tree
git restore --staged <file>Remove from staging area
git reset --soft HEAD~1Undo commit, keep staged
git reset HEAD~1Undo commit, keep in working tree
git reset --hard HEAD~1Undo commit, discard changes
git revert <commit>Create undo commit (safe)
git stashTemporarily save work
git stash popRestore saved work

Next Steps