Fundamentals 9 min read

Git Revert and Reset: A Rescue Guide for Mistakes

This article explains the differences between git reset, revert, and checkout, details the three reset modes (--soft, --mixed, --hard), shows when to use each command safely, provides practical scenarios, introduces reflog for recovery, and warns about common pitfalls.

Coder Trainee
Coder Trainee
Coder Trainee
Git Revert and Reset: A Rescue Guide for Mistakes

Three rollback commands and their differences

git reset

: moves HEAD and branch pointer; may change history; working‑tree effect depends on mode. git revert: creates a reverse commit; does not change history; safe for pushed commits. git checkout: switches branch or restores files; does not change history; overwrites files when used for restore.

git reset modes

--soft

Undo the last commit while keeping the index and working tree.

git reset --soft HEAD~1
# HEAD → previous commit
# index unchanged
# working tree unchanged

Use when the commit message is wrong.

git commit -m "wrong"
git reset --soft HEAD~1
git commit -m "feat: correct message"

--mixed (default)

Undo a commit, clear the index, keep the working tree.

git reset HEAD~1   # same as git reset --mixed HEAD~1
# HEAD → previous commit
# index cleared
# working tree unchanged

Use when unwanted files were added.

git add .
git commit -m "feat: new feature"
# realize a config file was added by mistake
git reset HEAD~1
git add src/
git commit -m "feat: new feature"

--hard

Reset HEAD, index and working tree to the given commit; discards uncommitted changes.

git reset --hard HEAD~1
# HEAD → previous commit
# index cleared
# working tree overwritten

Use when the code is broken and changes can be discarded.

# discard all local changes (not pushed)
git reset --hard HEAD
# go back three commits
git reset --hard HEAD~3

Risk: changes are unrecoverable.

git revert – safe undo

Principle

git revert HEAD

Creates a new commit whose content is the inverse of the specified commit, preserving history.

# original: A → B → C → D (HEAD)
# after git revert C:
# A → B → C → D → -C (new HEAD)

revert vs reset comparison

History: revert preserves history; reset rewrites it.

Pushed commits: revert is safe; reset requires force‑push.

Collaboration: revert is safe; reset may cause conflicts.

Undo any commit: both possible, but reset is risky.

# safe (already pushed)
git revert HEAD
git push origin main

# unsafe (already pushed)
git reset --hard HEAD~1
git push --force   # dangerous for teammates

Practical scenarios

Scenario 1 – commit not pushed

# amend commit message
git commit -m "wrong"
git commit --amend -m "feat: correct message"

# undo commit but keep changes
git reset --soft HEAD~1

# undo commit and re‑select files
git reset HEAD~1
git add src/
git commit -m "feat: new feature"

Scenario 2 – already pushed

# safe undo
git revert HEAD
git push origin main

# revert a specific historic commit
git revert <commit-hash>
git push origin main

Scenario 3 – accidentally deleted a file (not committed)

# restore from latest commit
git checkout -- file.txt
# or explicitly from HEAD
git checkout HEAD -- file.txt

Scenario 4 – accidentally deleted a file (committed)

# restore from previous commit
git checkout HEAD~1 -- file.txt
git add file.txt
git commit -m "fix: restore deleted file"

Scenario 5 – recover a commit lost by reset

# view operation history
git reflog
# example output:
# f1a2b3c HEAD@{0}: reset: moving to HEAD~1
# d4e5f6g HEAD@{1}: commit: feat: add payment feature

# reset to the discarded commit
git reset --hard d4e5f6g

reflog – rescue method

git reflog

records every movement of HEAD locally (default retention 90 days).

# list history
git reflog

# restore to state from 5 minutes ago
git reset --hard HEAD@{5}

# restore to a specific commit
git reset --hard <commit-hash>

Common pitfalls

Using reset --hard on pushed commits

# wrong
git reset --hard HEAD~1
git push --force

# correct
git revert HEAD
git push

Undoing a revert

# revert creates a commit; revert the revert restores original state
git revert HEAD   # first revert
git revert HEAD   # second revert restores
# or, if not pushed, simply reset
git reset --hard HEAD~1

Forgetting reflog

# after regrettable reset --hard
git reflog
# find previous commit hash
git reset --hard <hash>

Quick reference

git reset --soft HEAD~1    # only move HEAD
git reset HEAD~1           # move HEAD + reset index
git reset --hard HEAD~1    # full reset (dangerous)
git revert HEAD            # safe undo (adds reverse commit)
git checkout -- file.txt   # restore a single file
git reflog                 # view operation history (rescue)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

gitCommand Lineversion controlcheckoutresetrevertreflog
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.