To undo something in Git safely, first identify where it is: unstaged in your working tree, staged in the index, committed locally, or already pushed to a shared remote. Then decide whether you want to keep the edits. Unstage to keep changes out of the next commit; restore to discard tracked edits; reset to revise local history; revert to reverse a shared commit without rewriting the shared sequence.
What “undo” changes in Git
Git does not have one universal undo operation. Saving, staging, committing, and pushing are separate transitions: saving writes a file, staging copies selected content into the index, committing records the staged snapshot in local history, and pushing uploads local commits to a remote. The right operation depends on which transition you want to reverse and whether the work must be preserved.
As an Amazon Associate I earn from qualifying purchases.
The editor and command-line Git act on the same repository state. An editor button is not a separate safety net; check what state it changes and verify the result in Git.
Recommended Free Tools
Choose an operation by state and goal
| Current state and goal | Action | Effect | Important caution |
|---|---|---|---|
| Staged content should remain edited but leave the next commit | git restore --staged <path> |
Updates the index; the working file stays edited | Check the path and confirm with git status. |
| Discard a tracked, unstaged edit | git restore <path> |
Restores the file from the index by default | Overwrites the local edit. Inspect or save the diff first if you may need it. |
| Revise a commit that exists only on your local branch | Amend or reset, according to the intended result | Changes local history; reset mode determines what happens to the index and working tree | Avoid casually rewriting commits others may already have. |
| Reverse a commit already shared with others | git revert <commit> |
Adds a new commit that reverses the earlier change | For a merge commit, a mainline parent must be selected. |
| A branch moved and a committed tip seems missing | Inspect git reflog, then create a branch at the wanted commit |
Makes an existing commit reachable again | Reflog is not a backup of uncommitted edits. |
How do I undo git add without losing my work?
Unstage the path; do not restore it. For example:
git restore --staged src/example.js
git status
This removes the selected content from the index while leaving the edited file in your working tree. It will not be included in the next commit unless you stage it again. The older documented form is git reset HEAD <path>. The Pro Git “Undoing Things” chapter covers git restore in the context of Git 2.23.0 and later; see Pro Git: Undoing Things.
#1 Best Overall
How do I discard an unstaged change?
For a tracked file, git restore <path> restores its contents from the index by default, overwriting the unstaged edits. First inspect the scope of your changes:
git status
git diff -- src/example.js
If you are certain the edit should be discarded, restore that path and check the status again:
Rank #2
git restore src/example.js
git status
If any part of the diff might be useful, save a copy or otherwise preserve it before restoring. Git does not guarantee recovery of overwritten, uncommitted work. This command is for tracked paths; it is not a general way to remove untracked files. A tracked path absent from the restore source can also be removed to make the working tree match that source, so verify the path and result. See the official git restore documentation.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchHow do I undo my last commit if it is local-only?
If the commit has not been shared, you can revise local history with amend or reset. Choose based on what should happen to the changes. Reset moves the current branch to another commit; its mode determines whether it also changes the index and working tree.
| Reset mode | Branch | Index | Working tree |
|---|---|---|---|
--soft |
Moves to the target commit | Unchanged | Unchanged |
--mixed (default) |
Moves to the target commit | Updated to match the target | Unchanged |
--hard |
Moves to the target commit | Updated to match the target | Updated too; tracked local changes can be overwritten |
For instance, git reset --soft HEAD~1 moves the branch back one commit while leaving the index and working tree as they were. That keeps the changes staged. Before using any reset, confirm the target commit and the state you want to retain; do not use --hard if you need the current tracked edits.
A commit made unreachable by moving a branch may remain findable in the reflog for a time, but that is not a recovery guarantee, especially for edits that were never committed. Read Pro Git: Reset Demystified and Pro Git: Maintenance and Data Recovery before using reset when the history or work is uncertain.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.How do I undo a pushed commit?
For a commit already shared with others, the usual safe choice is git revert <commit>. Revert records an inverse change in a new commit, leaving the earlier commit in the shared history. This avoids moving a branch pointer in a way that would rewrite the sequence collaborators may have based work on.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →git revert <commit>
git status
git log --oneline -5
The normal revert operation expects a clean working tree. If conflicts occur, resolve them and continue or abort as appropriate; consult the official git revert documentation. Reverting a merge commit requires choosing a mainline parent, and it affects how later merges behave. Follow your team’s history policy and the documentation rather than treating a merge revert like an ordinary single-parent commit.
Best Value
Can an editor undo the same Git actions?
Yes, an editor can expose actions for unstaging, amending, undoing a last commit, reverting a shared commit, or discarding changes. The precise behavior depends on the editor and its current documentation. For example, VS Code documents “Undo Last Commit” as keeping the changes staged, while discarding uncommitted edits may not be recoverable. Check the action’s description, then verify the repository state with git status; for history changes, inspect the relevant log as well. See VS Code: Staging and committing changes and VS Code: Source Control overview.
How can I find a commit after moving a branch?
If a committed tip disappeared after a reset or another branch move, inspect the reflog for the earlier branch position:
git reflog
Identify the commit you want, then create a branch at it so the commit is reachable by a named reference:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsgit branch recovery <commit>
Use the actual commit identifier shown in your reflog. Reflog entries are local records, not permanent backups, and unreachable objects may eventually be pruned. Reflog cannot restore edits that were never committed.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




