git revert vs reset vs restore: Which to Use
Three Git commands that all "undo" things, and picking the wrong one on a shared branch is how you ruin a teammate's afternoon. Here's how to keep them straight.
Key takeaways
- Use git revert for commits that are already pushed, because it adds a new commit that undoes the old one without rewriting history.
- Use git reset to reshape local commits you have not pushed yet.
- Use git restore to discard or unstage file changes; it never touches commits or moves your branch.
On this page
Use git revert for anything already pushed: it adds a new commit that cancels the bad one and leaves shared history alone. Use git reset to move your branch pointer back over local, unpushed commits. Use git restore to throw away or unstage file changes. The first question is always "has this left my machine?"
Verified against
Git 2.56.0 (latest release listed on git-scm.com), against the git-scm.com reference pages for git-revert, git-reset, git-restore and git-reflog. Checked October 2026.
The three commands sound like synonyms. They are not. Reach for the wrong one and you either lose work or rewrite history that other people have already pulled. Both mistakes are common, so get the distinctions clean before you type anything with --hard in it.
The short version: revert undoes a commit by making a new commit. reset moves your branch pointer backwards. restore throws away changes in files. That's the whole map. The rest is knowing which situation you're in.
The mental model: HEAD, index, working tree
Every one of these commands moves content between three places, and if you hold that picture in your head the flags stop being cryptic.
- HEAD: the commit your branch currently points at. The last thing you committed.
- Index (also called the staging area): what goes into your next commit.
git addputs things here. - Working tree: the actual files on disk you're editing right now.
Work flows from working tree to index to HEAD when you save. These three commands move it the other direction, and they each stop at a different layer. Keep that in mind and the flags below read like plain English.
git revert: undo safely, in public
git revert <commit> creates a new commit that is the inverse of the one you name. If a commit added a line, the revert removes it. Nothing gets erased, the history stays intact, and everyone who already pulled the bad commit just gets a follow-up commit that cancels it out.
$ git revert a1b2c3d # undo one commit, opens an editor for the message
$ git revert HEAD # undo the most recent commit
$ git revert --no-commit a1b2c3d b4c5d6e # stage several undos, commit them together
This is the command for anything already pushed. It's non-destructive and it plays fair with your teammates. The only downside people complain about is the extra commit in the log, which is a strange thing to be upset about when the alternative is rewriting shared history. One caveat from the docs: reverting a merge commit needs -m 1 (or -m 2) to say which parent is the mainline, and it does not undo the merge's history, only its changes.
git reset: move the pointer, quietly
git reset drags your branch pointer to a different commit. Where it stops among those three layers depends on the flag:
$ git reset --soft HEAD~1 # undo commit, keep changes staged in the index
$ git reset --mixed HEAD~1 # undo commit, keep changes in working tree (the default)
$ git reset --hard HEAD~1 # undo commit, discard changes entirely
--soft is our go-to for "I committed too early." It rewinds the commit but leaves everything staged, ready to recommit properly. --mixed, the default, unstages too, so you're back to edited-but-not-added. --hard also wipes the working tree, which is exactly as final as it sounds. For the narrow "undo my last commit" case, undoing the last Git commit walks through each variant.
Because reset rewrites where the branch points, it's a local-only tool. Run it on commits you've pushed and your branch diverges from the remote, so your next push gets rejected (the error in fixing a rejected git push) and the "fix" is a force-push that stomps on whatever your colleagues built on top. Don't. Reset is for cleaning up your own laundry before anyone sees it.
git restore: for files, not commits
git restore is the newer command carved out of the overloaded git checkout, and it only deals with file contents. No commits, no branch pointers.
$ git restore config.yml # discard working-tree edits to one file
$ git restore . # discard all unstaged edits (careful)
$ git restore --staged config.yml # unstage, keeping the edits
$ git restore --source=HEAD~2 app.py # pull a file's older version back
If you've ever fumbled git checkout -- file and worried you were about to switch branches instead, restore is the fix. It does one job. Discarding working-tree changes and unstaging files used to share a command with branch switching, which confused everyone; now they're split (switch handles branches, restore handles files).
The decision table
| Situation | Command | Rewrites history? | Safe after push? | Can lose work? |
|---|---|---|---|---|
| Bad commit already pushed or on a shared branch | git revert | No | Yes | No |
| Local commits you haven't pushed, want to reshape | git reset --soft or --mixed | Yes | No | No |
| Local commits you want gone, changes and all | git reset --hard | Yes | No | Yes (reflog may save you) |
| Uncommitted file edits you want gone | git restore | No | Yes | Yes, uncommitted edits are unrecoverable |
| Staged something by mistake | git restore --staged | No | Yes | No |
| Need an old version of one file | git restore --source=<commit> | No | Yes | Overwrites the current file |
Read it top to bottom. The first question is always "is it pushed?" If yes, you're in revert territory and the conversation is over. If it's local and about commits, reset. If it's about files rather than commits, restore.
The reflog is your seatbelt
The part that makes reset --hard survivable on your own branch: Git rarely deletes commits immediately. It just stops pointing at them. git reflog shows every position HEAD has held, including the commit you thought you nuked.
$ git reflog # find the commit you lost, note its hash
$ git reset --hard a1b2c3d # or: git switch -c rescue a1b2c3d
We've recovered from a panicked --hard more than once this way. It's not infinite. Per the git-reflog docs, entries for commits no longer reachable from a branch expire after gc.reflogExpireUnreachable, which defaults to 30 days, and other entries after gc.reflogExpire, 90 days by default. Knowing reflog exists is the difference between "oh no" and "give me thirty seconds." What reflog cannot save is uncommitted work: git restore and reset --hard on edits you never committed are gone for good.
Questions people ask
What is the difference between git revert and git reset?
git revert creates a new commit that applies the inverse of an earlier one, so history only grows and anyone who already pulled is unaffected. git reset moves the branch pointer to an older commit, effectively removing later commits from the branch. That rewrites history, so it is safe only on commits that have never been pushed. Use revert on shared branches, reset on private ones.
Can I undo a git reset --hard?
Usually, if the work was committed. Run git reflog, find the hash HEAD pointed at before the reset, and run git reset --hard <hash> or create a branch from it. Unreachable reflog entries expire after 30 days by default. Changes that were never committed or staged are not recorded anywhere, so a hard reset over uncommitted edits cannot be undone.
Is git restore the same as git checkout?
For files, yes in effect. git restore <file> does what git checkout -- <file> did: it overwrites working-tree edits with the version from the index or a chosen commit. The difference is scope. git checkout also switches branches, which made typos dangerous, while git restore only touches file contents and git switch handles branches.
The call we'd make
Default to revert the instant anything has left your machine. It costs you one extra commit and buys you a history nobody has to untangle later. Save reset for tidying local work before you push, lean on --soft far more than you think, and treat reset --hard on a shared branch as something you simply do not do. Use restore for files, because that's what it's for and it says what it means. And whenever the panic rises, run git status and git reflog first. If you want the broader map of Git messing with your day, our Git troubleshooting guide covers the rest.
Sources
Cite this article
Kiril Urbonas (2026). git revert vs reset vs restore: Which to Use. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/git-revert-vs-reset
[git revert vs reset vs restore: Which to Use](https://www.devopsness.com/blog/git-revert-vs-reset) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from DevOps
Explore more articles in this category
kaniko CVE-2026-105769: Lookalike ACR Hosts Get Your Azure Token. Rebuild on v1.25.20
A FROM line pointing at a host like x.azurecr.io.attacker.com is enough to make kaniko hand the build's Entra ID token to that host. Which kaniko forks and images are affected, how to find Azure identities in your build pods, and what to rotate.
Sign Container Images With Cosign: Keyless GitHub Actions Workflow, SBOM and In-Cluster Checks
One workflow that builds, signs keylessly with GitHub OIDC, attaches an SBOM and SLSA provenance, plus the Kyverno policy that refuses anything unsigned. Copy it, change three names, ship it.
GitLab CVE-2026-110264: A Second CVSS 10 RCE in Nine Days. Patch to 19.5.1
An unauthenticated regex-compiler RCE reaching back to GitLab 7.0, no workaround, and backports on older branches that fix only the critical bug. The exact version per branch and the commands to get there.
You might have missed
Evergreen posts worth revisiting.