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.

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
# 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# 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
| Command | Moves branch | Resets index | Rewrites working tree | History |
|---|---|---|---|---|
reset --soft | Yes | No | No | Rewritten (local) |
reset --mixed | Yes | Yes | No | Rewritten (local) |
reset --hard | Yes | Yes | Yes, discards changes | Rewritten (local) |
reset --keep | Yes | Partial | Preserved where safe | Rewritten (local) |
reset <path> | No | Yes, for that path | No | Unchanged |
revert <sha> | Advances forward | No | No | Adds a commit |
restore | No | Optional | Yes | Unchanged |
clean | No | No | Deletes untracked | Unchanged |
| Goal | Correct command |
|---|---|
| Fix the last commit message | git commit --amend |
| Undo the last commit, keep the work | git reset --soft HEAD~1 |
| Undo the last commit, unstage the work | git reset HEAD~1 |
| Undo a pushed commit | git revert <sha> |
| Discard edits in one file | git restore <file> |
| Unstage one file | git restore --staged <file> |
| Bring one file back from an old commit | git restore --source=<sha> <file> |
| Delete untracked build output | git 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}.
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 .:
git rm --cached -r . # unstage everything in an unborn branchPrefer 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.
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.
git switch feature/login
git reset --keep origin/feature/login5. 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:
git reflog # find the pre-reset tip
git reset --hard HEAD@{1}
git push --force-with-lease origin mainIf 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.
# 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
| Symptom | Cause | Fix |
|---|---|---|
| "error: failed to push some refs" after reset | Reset rewound a pushed branch | git revert, or --force-with-lease if you own the branch |
| Uncommitted work gone | reset --hard without a stash | Not recoverable; use --keep or stash next time |
| "fatal: ambiguous argument 'HEAD'" | reset <path> in a repository with no commits | git rm --cached -r . |
| Reverted merge, feature cannot be re-merged | Merge still in history | git revert the revert |
reset --hard left build files behind | Untracked files are not part of reset | git clean -fd, after -n |
Deleted .env with clean -fdx | -x includes ignored files | Restore from backup; commit a .env.example |
| Reset the wrong branch | HEAD was on main, not the feature | git reflog, reset back, switch first next time |
| Revert conflicted on a range | Newest-first order not preserved | git revert --no-commit in reverse, one commit |
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit config: Aliases, rerere, and Credential Helpers
Five config scopes decide one value at a time. Aliases are user-level shorthand, rerere records conflict resolutions, and credential helpers choose where your secret lives.
Git Hooks: pre-commit, commit-msg, and Husky Automation
Hooks live in .git/hooks, which a clone never receives, so core.hooksPath and Husky are how they travel. Exit codes abort the operation, and every hook is bypassable.
Git bisect: Binary Search, Automated Scripts, and Bug Hunting
Mark one bad and one good commit and Git halves the range each step. bisect run automates it through exit codes, and monotonicity is the assumption that must hold.
Git ignore: Patterns, Negation, and Untracking Cached Files
Patterns are relative and last match wins, but a negation cannot revive a file inside an excluded directory. Ignore rules never touch tracked files: rm --cached first.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.