Git Reset vs Revert vs Restore: Differences, Examples and Which to Use
Git reset vs revert vs restore explained with a comparison table, safe commands for each undo scenario, and how to recover if you reset too far.
Table of contents
- Git reset vs revert vs restore: the quick comparison
- Three places Git keeps your work
- git restore: undo changes to files
- git reset: move the branch (and optionally the index and files)
- git revert: undo a commit by adding a new one
- Which command for which situation?
- How to recover after a bad reset
- Common mistakes to avoid
Git reset vs revert vs restore comes down to what you want to undo. git restore undoes changes to files — it discards edits in your working tree or unstages them — and never touches commit history. git reset moves your branch back to an earlier commit and can also reset the staging area and files, which rewrites history. git revert leaves history alone and adds a new commit that cancels an earlier one, which makes it the only safe choice for commits already pushed to a shared branch.
The names sound interchangeable, and the official manuals cross-reference each other, which is why so many developers reach for the wrong one. This guide puts the three side by side, then maps the common "I need to undo…" situations to the exact command, including how to get back commits after a reset goes too far. It continues our Git series, which started with git merge vs rebase.
Git reset vs revert vs restore: the quick comparison
git restore |
git reset |
git revert |
|
|---|---|---|---|
| Works on | Files (working tree and/or staging area) | The current branch pointer (plus index and files) | Commits |
| Changes commit history | No | Yes — commits after the target drop off the branch | No — adds a new commit |
| Safe on pushed/shared branches | Yes | No (needs a force-push) | Yes |
| Can lose uncommitted work | Yes, when restoring the working tree | Yes, with --hard |
No (it requires a clean working tree) |
| Typical command | git restore --staged app.py |
git reset --soft HEAD~1 |
git revert a1b2c3d |
| Use it to | Unstage or discard edits to files | Undo or reshape local, unpushed commits | Undo a commit others may already have |
Use git restore when the problem is in your files and nothing has been committed yet: you staged the wrong file, or you want to throw away edits.
Use git reset when the problem is in commits that exist only on your machine: the last commit was premature, you want to squash a few commits, or you want the branch to point somewhere else.
Use git revert when the commit is already pushed to a branch other people use, or when you want the undo itself to be recorded in history.
Three places Git keeps your work
All three commands make sense once you picture the three "areas" Git works with:
- The working tree — the files you edit on disk.
- The index (staging area) — what will go into the next commit, filled by
git add. - HEAD and the branch — the commit you are on, and the history behind it.
git restore copies content into the working tree, the index, or both. git reset moves the branch (and HEAD with it) and then optionally rewrites the index and working tree to match. git revert works one level up: it calculates the inverse of a commit's changes and commits that inverse on top of the branch.
git restore: undo changes to files
git restore arrived in Git 2.23 together with git switch, splitting the two jobs that git checkout used to do. Per the git-restore documentation, it restores the working tree by default; --staged (-S) restores only the index, and passing both --staged --worktree (-SW) restores both. When you don't name a source, it takes the content from the index for the working tree and from HEAD for --staged.
# Discard unstaged edits to one file (back to the staged/committed version)
git restore app.py
# Discard edits to everything in the current directory
git restore .
# Unstage a file but keep your edits
git restore --staged app.py
# Unstage AND discard edits in one go
git restore --staged --worktree app.py
# Bring back a file as it was two commits ago (staged copy is left alone)
git restore --source=HEAD~2 app.py
# Pick individual hunks to discard
git restore -p app.py
Two things to watch:
- Discarded edits are gone. Restoring the working tree overwrites uncommitted changes, and Git has no record of them. If you might want them later, run
git stashfirst. - It does not touch untracked files. A new file you never added is not "restored" away; delete it yourself, or use
git clean -nto preview andgit clean -fto remove untracked files.
git reset: move the branch (and optionally the index and files)
git reset <commit> points your current branch at <commit>. Commits that came after it are no longer on the branch. What happens to the index and working tree depends on the mode, as described in the git-reset manual:
| Mode | Branch/HEAD | Index (staging) | Working tree | Typical use |
|---|---|---|---|---|
--soft |
Moved | Unchanged | Unchanged | Redo the last commit(s) with changes still staged |
--mixed (default) |
Moved | Reset to match target | Unchanged | Uncommit and unstage, keep edits |
--hard |
Moved | Reset to match target | Overwritten to match target | Throw away commits and local changes |
--keep |
Moved | Reset to match target | Updated, aborts if it would overwrite local edits | A safer alternative to --hard |
# Undo the last commit, keep its changes staged
git reset --soft HEAD~1
# Undo the last commit, keep its changes as unstaged edits
git reset HEAD~1
# Squash the last three commits into one
git reset --soft HEAD~3
git commit -m "Add user profile page"
# Throw away the last commit and all uncommitted changes (destructive)
git reset --hard HEAD~1
# Make your local main identical to the remote (destructive)
git fetch origin
git reset --hard origin/main
HEAD~1 means "one commit before HEAD"; HEAD~3 means three back. You can also give a commit hash from git log --oneline.
There is also a file form, git reset <file>, that leaves the branch where it is and only copies the file's HEAD version into the index — that is, it unstages it. The manual describes it as equivalent to git restore --staged <file>, so you can use whichever reads better to you.
The rule: only reset commits you have not pushed, or that only you use. If you reset a pushed branch, your next push will be rejected, and forcing it (even with --force-with-lease) rewrites history for everyone who pulled — the same trade-off covered in the golden rule of rebasing.
git revert: undo a commit by adding a new one
git revert <commit> works out the opposite of what that commit changed and records it as a new commit. The original stays in history, so the branch only moves forward and nobody else has to repair their clone. The git-revert manual notes it needs a clean working tree, so commit or stash your edits first.
# Revert the most recent commit (opens your editor for the message)
git revert HEAD
# Revert a specific commit and accept the default message
git revert --no-edit a1b2c3d
# Revert several commits but combine the result into one commit
git revert --no-commit a1b2c3d e4f5g6h
git commit -m "Revert broken payment changes"
# Revert a range (here: the commits after main~5 up to main~2)
git revert --no-commit main~5..main~2
git commit -m "Revert experimental cache layer"
# Push as normal — no force needed
git push
If a revert hits a conflict, fix the files, git add them and run git revert --continue; git revert --abort cancels and returns to where you started.
Reverting a merge commit
A merge commit has two parents, so Git needs to know which side is the "mainline" to keep. On a branch like main, that is almost always parent 1:
git revert -m 1 <merge-commit-hash>
The manual warns that reverting a merge declares you will never want those changes again. If you later fix the feature branch and merge it a second time, Git considers the earlier commits already merged and leaves them out; you would need to revert the revert first (git revert <revert-commit>) before merging again.
Which command for which situation?
| I want to… | Command |
|---|---|
| Unstage a file | git restore --staged <file> |
| Discard my uncommitted edits to a file | git restore <file> |
| Get a file back from an older commit | git restore --source=<commit> <file> |
| Fix the message of the last (unpushed) commit | git commit --amend |
| Undo the last unpushed commit, keep the changes | git reset --soft HEAD~1 or git reset HEAD~1 |
| Delete the last unpushed commit entirely | git reset --hard HEAD~1 |
| Undo a commit that is already pushed | git revert <commit> |
| Undo a pushed merge | git revert -m 1 <merge-commit> |
| Make my branch match the remote exactly | git fetch then git reset --hard origin/<branch> |
When in doubt, ask one question: has anyone else seen this commit? If yes, revert. If no, reset is fine. If there is no commit involved at all, restore.
How to recover after a bad reset
A reset does not delete commits straight away; it just stops the branch from pointing to them. Git records the previous tip in ORIG_HEAD and keeps a log of every position HEAD has had in the reflog:
# Immediately after a reset you regret
git reset --hard ORIG_HEAD
# Or find the right point in the reflog
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~2
# 9f8e7d6 HEAD@{1}: commit: Add search endpoint
git reset --hard 'HEAD@{1}'
Unreachable commits are eventually removed by garbage collection, so recover sooner rather than later. What the reflog cannot bring back is uncommitted work wiped by git reset --hard or git restore — that was never stored in a commit. Before any destructive command, git stash (or a throwaway commit) costs seconds and removes the risk.
Common mistakes to avoid
- Running
git reset --hardto unstage a file. It also wipes your edits. Usegit restore --staged. - Resetting a shared branch. Revert instead; a force-push on
maincauses problems for everyone who already pulled. - Using
git revertto discard uncommitted work. Revert only undoes commits; for uncommitted changes the manual points to reset (or, for files, restore). - Forgetting
-mon a merge revert. Git refuses to revert a merge without being told the mainline parent. - Mixing up
HEAD~1andHEAD^1on merges. For ordinary commits they are the same; on a merge,^2means the second parent, while~2means two generations back.
Undo commands are a lot less scary once your remote setup is reliable — if pushing still asks for a password, set up SSH keys for GitHub or GitLab. And if you are versioning a project's environment as well as its code, the same "don't commit what you can rebuild" thinking applies to Python virtual environments and Docker images.
Frequently asked questions
+What is the difference between git reset, git revert and git restore?
git restore changes files (in the working tree or the staging area) and never touches commits. git reset moves the current branch to another commit, optionally also resetting the staging area and files. git revert keeps history intact and adds a new commit that undoes an earlier one.
+Should I use git reset or git revert to undo a pushed commit?
Use git revert. It adds a new commit that cancels the old one, so anyone who already pulled the branch can simply pull again. git reset rewrites history, which forces a force-push and breaks other people's clones of a shared branch.
+How do I unstage a file in Git?
Run git restore --staged <file>. It copies the version from HEAD back into the staging area and leaves your edits in the working tree. The older git reset <file> (or git reset HEAD <file>) does the same thing.
+How do I undo the last commit but keep my changes?
If the commit has not been pushed, run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 (the default --mixed mode) to keep them as unstaged edits. If it has been pushed, use git revert HEAD instead.
+Can I recover commits after git reset --hard?
Usually, yes. Git sets ORIG_HEAD before a reset, and git reflog lists where HEAD has been, so git reset --hard ORIG_HEAD or git reset --hard HEAD@{1} returns the branch to its earlier tip. Uncommitted changes discarded by --hard cannot be recovered this way.
+Is git restore the same as git checkout?
For files, yes. git restore was split out of git checkout so that restoring files and switching branches (now git switch) are separate commands. git restore --source=HEAD --staged --worktree <file> is the documented equivalent of git checkout HEAD -- <file>.
Sources & references
- Git documentation — git-reset (2.54)
- Git documentation — git-revert (2.54)
- Git documentation — git-restore
- Pro Git book — 3.6 Git Branching: Rebasing
Spotted an error? See our Corrections Policy.
- #git
- #version-control
- #git-reset
- #git-revert
- #git-restore
- #developer-tools
Dipak SuryawanshiFounder & Editor
Dipak Suryawanshi is a backend developer who works with Java and Spring Boot, focused on building APIs, clean architectures and developer tools. He founded KernelBrief, an independent technology publication based in India, sets its editorial direction and is responsible for everything published on it. Every KernelBrief article is researched and checked against primary sources such as official documentation, vendor advisories and release notes before it is published.
Related articles
Git Merge vs Rebase: Key Differences, Examples and When to Use Each
Git rebase vs merge explained: a side-by-side comparison, how each combines branches, when to use which, and safe commands for pull, conflicts and force-push.
· 8 min read
Python Virtual Environments: A Practical Guide to venv (and Fixing 'externally-managed-environment')
Create, activate and manage Python virtual environments with venv on Windows, macOS and Linux — and fix pip's externally-managed-environment error.
· 6 min read
Spring Boot Global Exception Handling with @RestControllerAdvice and ProblemDetail
Spring Boot global exception handling with @RestControllerAdvice and ProblemDetail (RFC 9457): consistent JSON errors, validation messages and a safe 500.
· 10 min read