Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git reset vs revert vs restore: Soft, Mixed, Hard, and Undoing Safely

Three commands with three depths: reset rewrites the branch, revert adds a commit, restore touches files. Which one is still safe after a push.

~4 min readGit
#Git#Undo#Recovery#CLI
3D illustration of three snapshot meters for soft, mixed and hard reset with a revert arrow

Quick Summary

Three commands that are confused because all of them "undo" something, at different depths. git reset moves the current branch pointer and optionally rewrites the index and working tree. git revert creates a new commit that reverses an old one, leaving history intact. git restore touches only files — index or working tree — and never moves a branch.

The reset modes differ only in how far the rewrite reaches: --soft moves the pointer only (changes stay staged), --mixed (the default) also resets the index (changes become unstaged), --hard also overwrites the working tree, discarding uncommitted edits permanently.

The rule that avoids most disasters: use revert on anything pushed, reset on anything local, and restore when you only mean files. reset --hard never deletes untracked files — that is git clean, which is why "I reset and my new files are still here" and "I reset and lost my work" are both true depending on what was tracked.

Basic Syntax & Core Signatures

bash
# Reset: move the branch, then decide how far the rewrite goes git reset --soft HEAD~1 # undo the commit, keep changes staged git reset HEAD~1 # undo the commit, unstage the changes (--mixed) git reset --hard HEAD~1 # undo the commit and discard the changes git reset --keep HEAD~1 # move the branch, keep local changes if safe git reset --hard origin/main # discard local commits, match the remote git reset file.txt # unstage one path (index only) # Revert: inverse commit, history preserved git revert HEAD git revert --no-edit 3f2a1b9 git revert -m 1 9c1e2f4 # revert a merge commit, mainline = parent 1 git revert HEAD~3..HEAD # revert a range, newest first # Restore: working tree and index only git restore file.txt # working tree <- index git restore --staged file.txt # index <- HEAD, keep working tree git restore --source=HEAD~2 file.txt # working tree <- an old commit git restore --staged --worktree file.txt # both git restore -p file.txt # hunk by hunk git restore --source=origin/main -- path/ # bring one directory back
bash
# Untracked files are not part of reset git clean -nd # dry run: show what would be removed git clean -fd # remove untracked files and directories git clean -fdx # also remove ignored files (build output, .env)

git clean -fdx is destructive and has no undo. Run -n first, always; the -x flag is the one that deletes .env files.

Key Comparison Table

CommandMoves branchResets indexRewrites working treeHistory
reset --softYesNoNoRewritten (local)
reset --mixedYesYesNoRewritten (local)
reset --hardYesYesYes, discards changesRewritten (local)
reset --keepYesPartialPreserved where safeRewritten (local)
reset <path>NoYes, for that pathNoUnchanged
revert <sha>Advances forwardNoNoAdds a commit
restoreNoOptionalYesUnchanged
cleanNoNoDeletes untrackedUnchanged
GoalCorrect command
Fix the last commit messagegit commit --amend
Undo the last commit, keep the workgit reset --soft HEAD~1
Undo the last commit, unstage the workgit reset HEAD~1
Undo a pushed commitgit revert <sha>
Discard edits in one filegit restore <file>
Unstage one filegit restore --staged <file>
Bring one file back from an old commitgit restore --source=<sha> <file>
Delete untracked build outputgit clean -fd

Practical Edge Cases & Deep Dives

1. reset --hard on a tracking branch is a local shortcut, not a sync

git reset --hard origin/main is common before a rebase or after abandoning local commits, and it reconciles nothing on the remote. It also leaves untracked and ignored files alone, and it does not touch commits that are only on the remote. The recoverable part is the commits: they stay in git reflog for 90 days (30 for unreachable objects), so a mistaken hard reset is usually undoable with git reset --hard HEAD@{1}.

bash
git reflog -5 git reset --hard HEAD@{1}

The part that is not recoverable is uncommitted work, because it was never in an object.

2. Unstaging with reset <path> versus restore --staged

Both unstage; reset refuses when the branch has no commits yet (no HEAD), which is exactly the state after git init plus git add .. restore --staged has the same problem, and the answer for a repository with no commits is git rm --cached -r .:

bash
git rm --cached -r . # unstage everything in an unborn branch

Prefer restore --staged in modern scripts for readability, and remember both keep the working tree untouched.

3. Reverting a merge, and why the change "comes back"

git revert -m 1 <merge> undoes the merge relative to parent 1. The reverted content is gone, but the merge commit is still in history, so a later git merge of the same branch does nothing: Git considers those commits already merged. Restoring the feature means reverting the revert.

bash
git revert -m 1 9c1e2f4 # undo the merge # later, to bring the feature back: git revert 1d2e3f5 # revert the revert (the new revert commit's sha)

Reverting a range. git revert A..B applies the inverses newest-first, because each revert is a normal commit that must apply on top of the current state. Reverting oldest-first usually conflicts with itself. For a chain of commits, git revert --no-commit B A C in reverse order plus one commit is often cleaner than a series of revert commits.

4. reset --keep and dirty trees

--keep moves the branch but refuses if an uncommitted change would be lost, which makes it the safest "move my branch without losing work" option. It is underused because the error looks like a failure; in practice it protects precisely the files that --hard would silently overwrite.

bash
git switch feature/login git reset --keep origin/feature/login

5. Recovering after reset --hard on a shared branch

If the reset was pushed, the remote no longer has the commits either. Restore from the local reflog, then push with a lease, and tell the team — their clones still have the commits and their next push will reintroduce them:

bash
git reflog # find the pre-reset tip git reset --hard HEAD@{1} git push --force-with-lease origin main

If the reflog is gone locally, git fsck --lost-found finds dangling commits, and a colleague's clone is the fastest source of truth.

6. Deciding between reset and revert in a team

The technical rule — local gets reset, shared gets revert — is not a style preference. A reset rewrites hashes that other people's branches are based on, so their next pull either fails or quietly reintroduces the commits you intended to remove. A revert adds a new commit that everyone can pull and see in review. The cost of revert is a noisier history and a two-step undo (revert the revert); the cost of reset is a broken clone that someone else has to fix.

bash
# shared branch: undo a bad deploy commit git revert --no-edit 3f2a1b9 git push origin main # feature branch nobody else has: tidy it up git reset --soft HEAD~3 git commit -m "feat(report): combine three WIP commits"

The pragmatic middle ground in many teams: reset freely on a branch you alone have pushed, revert anything merged into the default branch, and never force-push over a commit you did not author.

Common Git Operation Mistakes

SymptomCauseFix
"error: failed to push some refs" after resetReset rewound a pushed branchgit revert, or --force-with-lease if you own the branch
Uncommitted work gonereset --hard without a stashNot recoverable; use --keep or stash next time
"fatal: ambiguous argument 'HEAD'"reset <path> in a repository with no commitsgit rm --cached -r .
Reverted merge, feature cannot be re-mergedMerge still in historygit revert the revert
reset --hard left build files behindUntracked files are not part of resetgit clean -fd, after -n
Deleted .env with clean -fdx-x includes ignored filesRestore from backup; commit a .env.example
Reset the wrong branchHEAD was on main, not the featuregit reflog, reset back, switch first next time
Revert conflicted on a rangeNewest-first order not preservedgit revert --no-commit in reverse, one commit

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.