How to Undo Almost Anything in Git
Reset, revert, restore, and reflog. Learn which command undoes which situation, and how to recover from a mistake safely.
By Velloreth
The three commands
Git has three distinct undo operations, and confusing them is the source of most mistakes.
| Command | What it changes | Rewrites history? |
|---|---|---|
git restore | Files in the working tree or index | No |
git reset | The branch pointer, index, and working tree | Yes |
git revert | Creates a new commit | No |
The rule: use restore for files, reset for local commits, and revert for pushed commits.
Undoing file changes
Discard changes in a file:
git restore file.js
This replaces the working tree version with the version from the index. The changes are lost.
Discard all changes:
git restore .
Unstage a file:
git restore --staged file.js
This removes the file from the index but keeps the working tree changes.
Restore from a specific commit:
git restore --source=HEAD~2 file.js
The older syntax:
git checkout -- file.js # discard changes
git reset HEAD file.js # unstage
git restore is the modern, focused command. git checkout still works but does too many things.
Undoing local commits
git reset moves the branch pointer and optionally changes the index and working tree.
--soft — move the pointer, keep changes staged.
git reset --soft HEAD~1
The commit is undone, and its changes are staged, ready to be recommitted. Use this to fix a commit message or combine commits.
--mixed (the default) — move the pointer, unstage changes.
git reset HEAD~1
The commit is undone, and its changes are in the working tree but not staged. Use this to re-split a commit into several.
--hard — move the pointer, discard changes.
git reset --hard HEAD~1
The commit and its changes are gone. This is destructive. Uncommitted work is lost.
Undo the last commit but keep the changes:
git reset --soft HEAD~1
Undo the last three commits:
git reset --soft HEAD~3
Undoing pushed commits
git revert creates a new commit that applies the inverse of an old one.
git revert a1b2c3d
This is the safe way to undo a pushed commit, because it does not rewrite history. Everyone else’s history stays valid.
Revert without committing (to combine several reverts):
git revert --no-commit a1b2c3d
Revert a merge commit:
git revert -m 1 <merge-commit>
The -m 1 says which parent to keep. This is needed because a merge commit has two parents, and Git does not know which side to revert to.
Reverting a revert. If you revert a commit and later want it back, revert the revert.
git revert <the-revert-commit>
Recovering with reflog
The reflog records every position of HEAD, which makes almost any mistake recoverable.
git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
e4f5g6h HEAD@{1}: commit: add validation
i7j8k9l HEAD@{2}: commit: add user model
Recover a reset:
git reset --hard e4f5g6h
Recover a deleted branch:
git reflog
# find the last commit on the branch
git branch recovered-branch e4f5g6h
Recover a bad rebase:
git reflog
# find the commit before the rebase
git reset --hard i7j8k9l
The reflog is local and keeps entries for around 90 days for reachable commits. It is the safety net for almost every mistake.
Undoing a merge
Before committing the merge:
git merge --abort
After committing the merge:
git reset --hard HEAD~1
This works only if the merge has not been pushed. If it has, use git revert -m 1.
Undoing a push
If you pushed a commit that should not be there, and nobody else has pulled it:
git reset --hard HEAD~1
git push --force-with-lease
--force-with-lease is safer than --force, because it refuses to push if the remote has changed since your last fetch. It prevents overwriting someone else’s work.
If others may have pulled it, use git revert instead. Rewriting a shared branch causes problems for everyone.
Undoing a stash
Apply and keep the stash:
git stash apply
Apply and remove the stash:
git stash pop
Recover a dropped stash:
git fsck --no-reflogs | grep commit
# find the stash commit, then
git stash apply <commit>
Dropped stashes are not immediately garbage collected, so they are often recoverable.
Undoing a file deletion
Restore a deleted file:
git restore file.js
Restore a file deleted in an old commit:
git checkout <commit>^ -- file.js
The decision tree
I changed a file and want it back:
git restore file.js
I staged something I did not mean to:
git restore --staged file.js
I committed too early:
git reset --soft HEAD~1
I committed and pushed, and nobody has pulled:
git reset --hard HEAD~1 && git push --force-with-lease
I committed and pushed, and others may have it:
git revert <commit>
I did something and I do not know what:
git reflog
Preventing the need to undo
The best undo is the one you do not need.
Commit often. A small commit is easy to revert. A large one that mixes several changes is not.
Check git status before destructive commands. The output tells you what is staged, what is modified, and what is untracked, which is exactly what a --hard reset would discard.
Use a branch for experiments. A branch is free, and abandoning it is trivial. Working directly on main makes every mistake a production problem.
Push often. A pushed commit is recoverable from the remote even if your local copy is lost.
Keep the reflog in mind. Almost every local mistake is recoverable, which is worth remembering before panicking.
Practical guidance
- Use
restorefor files,resetfor local commits,revertfor pushed commits. - Prefer
--softover--hardwhen you might want the changes. - Use
--force-with-lease, never plain--force. - Use
git reflogwhen you are lost. - Never
--hardon uncommitted work you might want. - Revert a merge with
-m 1. - Revert a revert to restore a reverted commit.
- Check
git statusbefore any destructive command.
For the related guides, see git-merge-vs-rebase and git-commit-messages-guide. For the broader context, git-branching-strategies and debugging-strategies-that-work.
Quotable summary
git restore undoes changes to files, git reset moves the branch pointer, and git revert creates a new commit that undoes an old one. git reset —soft keeps changes staged, —mixed unstages them, and —hard discards them entirely, which is destructive. git revert is the safe way to undo a pushed commit because it does not rewrite history, while reset on a pushed commit forces everyone else to reconcile a diverged history. git reflog records every position of HEAD, so almost any mistake is recoverable, including a bad rebase, a deleted branch, or a hard reset. Never use —hard on changes you have not committed unless you are certain you do not want them, and use —force-with-lease rather than —force when you must rewrite a remote branch.