← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Git Revert vs Reset vs Restore

git Intermediate

Also Known As

git revert git reset git restore undo commit

TL;DR

Three different ways to undo in git — revert creates a new undo commit (safe for shared branches), reset moves HEAD (rewrites history, dangerous on shared branches), restore undoes working directory changes.

Explanation

git revert <hash>: creates a new commit that undoes the changes — safe for shared branches, preserves history. git reset HEAD~1: moves HEAD back, three modes — --soft (keep changes staged), --mixed (keep changes unstaged, default), --hard (discard changes entirely). git restore: undoes working directory or index changes without touching commit history. Rule: never use reset --hard on commits pushed to a shared branch — use revert instead. git reflog recovers from accidental hard resets.

Diagram

flowchart LR
    subgraph git revert - Safe
        C1[A] --> C2[B] --> C3[C - bug]
        C3 --> C4[Revert of C<br/>new commit]
        NOTE1[History preserved<br/>safe on shared branches]
    end
    subgraph git reset - Rewrites History
        D1[A] --> D2[B] --> D3[C - bug]
        D3 -.->|reset moves HEAD back| D2
        NOTE2[C removed from history<br/>dangerous if pushed]
    end
    style NOTE1 fill:#238636,color:#fff
    style NOTE2 fill:#f85149,color:#fff

Common Misconception

git reset --hard is the same as git revert — reset rewrites history by moving HEAD; revert adds a new commit. Using reset --hard on a shared branch forces all collaborators to reset too.

Why It Matters

git reset --hard on a branch shared with teammates forces them to do git reset --hard too or end up with conflicts — revert is always safe on shared branches.

Common Mistakes

  • git reset --hard on a pushed branch — rewrites public history, breaks collaborators.
  • git revert when you meant reset — revert on a local-only branch adds unnecessary noise.
  • Not knowing that git restore --staged unstages without losing changes.
  • Using reset to 'undo' a merge instead of git revert -m 1 <merge-hash>.

Code Examples

✗ Vulnerable
# Dangerous: reset on shared branch:
git reset --hard HEAD~3  # Removes 3 commits locally
git push --force         # Rewrites shared history
# Teammates: their local branches now diverge
# They must reset or face complex merges
✓ Fixed
# Safe: revert on shared branch:
git revert abc123      # Creates new 'undo' commit — history preserved
git push               # Safe — no history rewrite

# Local cleanup only (never pushed):
git reset --soft HEAD~1   # Undo commit, keep changes staged
git reset --mixed HEAD~1  # Undo commit, keep changes unstaged
git reset --hard HEAD~1   # Undo commit AND discard changes (careful!)

# Undo working directory changes:
git restore src/file.php  # Discard unstaged changes to one file
git restore --staged src/ # Unstage without losing changes

Added 16 Mar 2026
Edited 22 Mar 2026
Views 21
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 3 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T
No pings yesterday
Amazonbot 7 Perplexity 5 Ahrefs 2 SEMrush 2 Majestic 1 Google 1
crawler 18
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Use git revert to undo commits on shared branches (creates a new commit, safe to push); use git reset only on local/private branches (rewrites history, dangerous to force-push)
📦 Applies To
git any
🔗 Prerequisites
🔍 Detection Hints
git reset --hard on shared branch then force push; lost commits from hard reset without backup; using revert when reset would be cleaner on local branch
Auto-detectable: ✗ No git
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File

✓ schema.org compliant