Every developer eventually types a Git command and immediately wishes they had not. The good news: Git has a precise git undo for almost every mistake. The bad news: there are three commands that sound alike, git restore, git reset and git revert, and picking the wrong one can throw away work or rewrite history your teammates already pulled.
This cheat sheet explains what each command changes, when to use it, and the exact commands for the mistakes people make most often. The short version: choose the undo that matches your intent.
Key takeaways
- git restore changes files or the staging area. It never moves your branch or touches history.
-
git reset moves your branch to another commit. Its mode (
--soft,--mixed,--hard) decides what happens to staged and working files. - git revert undoes a commit by adding a new commit, so it is the safe choice for anything already pushed.
- git reflog is your safety net: it can bring back commits you "lost" with a reset.
Ask three questions to pick the right git undo
The cheat sheet works like a small decision tree. Go through the questions in order and stop at the first "yes":
-
Do you only need to fix file contents or unstage a file? Use
git restore. -
Do you need to move the branch (HEAD) to another commit? Use
git reset. -
Do you need to undo a commit but keep shared history intact? Use
git revert.
To see why this works, it helps to know that your work lives in three places: the working tree (the files you edit), the staging area or index (what git add prepared for the next commit), and the commit history with HEAD pointing at your current branch tip. Each undo command acts on a different part of that picture.
Where each git undo command acts: restore changes files, reset moves the branch, revert adds a new commit.
git restore: fix files and unstage changes
git restore was added in Git 2.23 to take over the file-related jobs that used to be done with git checkout. It is non-history changing : it only affects file contents and the staging area, and it never moves your branch.
# Throw away unstaged edits in a file (restores it from the staging area)
git restore app.js
# Unstage a file but keep your edits in the working tree
git restore --staged app.js
# Discard staged AND unstaged changes: back to the last commit
git restore --staged --worktree app.js
# Get the version of a file from an older commit
git restore --source=HEAD~2 app.js
One detail catches people out: plain git restore app.js restores the file from the staging area, not from the last commit. If you already ran git add, the staged version comes back. Add --staged --worktree to go all the way back to HEAD.
Warning: Restoring the working tree throws away uncommitted edits, and Git keeps no copy of them. Run git diff app.js first if you are not sure what you are about to lose.
Good for: discarding working-tree changes, or removing a file from staging before you commit.
git reset: move the branch to another commit
git reset <commit> moves your current branch (and HEAD) to a different commit. Commits after that point are no longer on the branch, which is why the cheat sheet marks it can rewrite history. What happens to your staged and working files depends on the mode:
| Mode | Branch / HEAD | Staging area | Working files |
|---|---|---|---|
| --soft | Moves | Kept as is | Kept |
| --mixed (default) | Moves | Reset, nothing staged | Kept |
| --hard | Moves | Reset | Overwritten (changes lost) |
Common reset recipes
# Undo the last commit but keep its changes staged (re-commit differently)
git reset --soft HEAD~1
# Undo the last commit and unstage its changes (default --mixed)
git reset HEAD~1
# Throw away the last commit AND all uncommitted changes
git reset --hard HEAD~1
# Safer alternative: stops instead of overwriting files you changed locally
git reset --keep HEAD~1
--soft is handy for squashing several small local commits into one: reset back a few commits and commit again. --mixed is the default when you give no mode. --hard is the most destructive option: tracked files are overwritten to match the target commit, and uncommitted changes in them are gone.
Tip: If you only want to fix the message or add a forgotten file to your last commit, you do not need reset at all. Stage the file and run git commit --amend.
Good for: rewriting local history that nobody else has, or moving back to an earlier commit on your own branch.
git revert: undo a commit safely in shared history
git revert <commit> does not delete anything. It creates a new commit that reverses the changes of the commit you name. History stays intact, so anyone who already pulled the branch can simply pull again. That makes it the right undo for main and any branch other people use.
# Undo one commit with a new "Revert ..." commit
git revert a1b2c3d
# Undo several commits but review before committing
git revert --no-commit a1b2c3d e4f5a6b
git commit -m "Revert the faulty payment changes"
# Undo a merge commit (keep parent 1, the branch you merged into)
git revert -m 1 9f8e7d6
# Stuck on a conflict during a revert?
git revert --continue # after fixing the files
git revert --abort # give up and go back
Reverting a merge has a side effect worth knowing: Git remembers that those commits were already merged. If you later want the same branch back, merging it again will not bring in the reverted changes, so you may need to revert the revert.
Good for: safely undoing a commit that is already pushed or shared.
Quick reference: which git undo for which mistake?
| Mistake | Command |
|---|---|
| Edited a file and want the old version back | git restore file |
| Staged a file by accident | git restore --staged file |
| Typo in the last commit message (not pushed) | git commit --amend |
| Last commit was wrong, keep the changes (not pushed) | git reset --soft HEAD~1 |
| Want to drop local commits and changes completely | git reset --hard <commit> |
| A pushed commit broke something | git revert <commit> |
| A pushed merge broke something | git revert -m 1 <merge-commit> |
| New untracked files you want gone |
git clean -n to preview, then git clean -f
|
Recover from a bad reset with git reflog
Reset a branch too far? The commits are usually not gone yet. Git records every move of HEAD in the reflog, so you can find the commit you were on and go back to it:
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: Add checkout page
git reset --hard HEAD@{1}
By default Git keeps reflog entries for 90 days, and 30 days for commits that are no longer reachable from a branch (the gc.reflogExpire and gc.reflogExpireUnreachable settings). The reflog only covers committed work, though. Changes you never committed or stashed cannot be recovered this way, which is one more reason to commit early and often.
Already pushed? Rules for shared branches
- Prefer revert on shared branches. It adds history instead of removing it, so nobody's clone breaks.
- Reset only what you alone own. Rewriting a pushed branch forces everyone who pulled it to repair their copy.
-
If you must rewrite a pushed branch (for example your own feature branch), push with
git push --force-with-leaserather than--force. It refuses to overwrite the remote if someone else pushed in the meantime. - Protect main. Branch protection rules on GitHub or GitLab block force pushes to important branches altogether.
Conclusion
Git undo stops being scary once you match the command to what you want to change: restore for files and staging, reset to move your branch, and revert to undo shared commits with a new commit. Keep git reflog in mind as your safety net and reach for --hard only when you are sure. The official manual pages for git restore, git reset and git revert cover every option in detail.
Building software and want a team that handles the engineering side for you? See our development services, browse what we have built, or tell us about your project.
Frequently Asked Questions
What is the difference between git reset and git revert?
git reset moves your branch to another commit, so the commits after it disappear from the branch and history is rewritten. git revert keeps history and adds a new commit that reverses an earlier one. Use reset for local work and revert for commits that are already pushed or shared.
How do I undo the last commit but keep my changes?
Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them as unstaged edits. Only do this if the commit has not been pushed; for a pushed commit, use git revert instead.
How do I unstage a file in Git?
Run git restore --staged followed by the file name. The file is removed from the staging area and your edits stay in the working tree. Older guides use git reset HEAD with the file name, which does the same thing.
Can I recover commits after git reset --hard?
Usually yes. Run git reflog to find the commit your branch pointed to before the reset, then reset back to it. By default Git keeps these entries for 30 to 90 days. Uncommitted changes that --hard overwrote cannot be recovered this way.
Is git restore the same as git checkout?
git restore was added in Git 2.23 to take over the file-restoring job of git checkout, while git switch took over changing branches. git checkout still works, but restore and switch are clearer about what they change.


Top comments (0)