Everyone who uses Git eventually types a commit they regret, stages the wrong file, or edits a file into a state worse than where it started. The good news is that Git is built for exactly these moments. Almost nothing committed in Git is truly lost, and uncommitted work can usually be put back with a single command. The confusion is not whether you can undo, but which of several undo commands fits your situation. Restore, reset, and revert sound similar, yet they operate on different things and carry very different levels of risk.
This guide sorts them by the question you are actually asking. Do you want to discard edits you have not committed yet? Do you want to unstage a file? Do you want to take back a commit that only exists on your machine, or one that teammates already have? Each answer leads to a different command. If the basic loop of status, add, and commit is still new, read our rundown of Git basic commands first, because every undo command below is described in terms of that loop.
The Three Places Changes Live
Undo commands make sense once you picture the three places a change can sit. The working tree is the actual files on your disk, where you edit. The staging area, also called the index, holds the changes you have queued with git add for the next commit. The history holds commits, the permanent snapshots. Each undo command moves changes between these places, or discards them from one of them, and the risk depends entirely on which place is involved. Discarding from the working tree is instant and, if the change was never staged or committed, final. Moving a commit is safe while it is private and disruptive once it is shared. Keep that map in mind and the choice of command usually makes itself.
Undo Uncommitted Edits with restore
The most common undo of all is the simplest: you edited a file, the edit was a mistake, and you want the last committed version back. Nothing has been staged or committed since the mistake. The restore command does exactly this job and nothing more.
git restore index.html git restore .
The first line discards every uncommitted change in the one named file and replaces it with the version from the last commit. The second line does the same for every modified file in the project. Read the warning in that description carefully: discards means discards. If the change was never staged and never committed, Git has no other copy of it, and restore removes it permanently. That is exactly what you want when the edit is a genuine mistake, and exactly what hurts when you restore the wrong file. Run git status and git diff first, confirm the file really holds the mistake and nothing valuable alongside it, and only then restore. Modern Git split restore out of the older checkout command to make this job clearer, so in older notes you may see checkout used for the same purpose; restore is the spelling to learn now.
Unstage a File with restore –staged
The next situation sits one step later. You staged a file with git add, then realised it does not belong in the next commit: perhaps it belongs in a separate commit, or it should not be committed at all. The file content is fine. You only want it out of the staging area. The same restore command, with the staged flag, handles this without touching the file itself.
git add notes.txt index.html git restore --staged notes.txt git status
Walk through the sequence. The first line stages two files together, a common slip when adding in a hurry. The second line unstages only the notes file: it removes that file from the queue for the next commit while leaving the edits in the file exactly as they are. The status line lets you confirm the result, showing the notes file back among the unstaged changes and the page file still staged. This undo is completely safe, because it moves nothing and deletes nothing; it only changes what the next commit will include. Old tutorials perform this step with a form of reset, which also works, but the staged restore spelling states its intent plainly and is the one Git itself suggests in status hints.
Take Back a Private Commit with reset
Now for commits. Suppose you committed too early, committed on the wrong branch, or wrote a message you want to redo, and crucially, you have not pushed yet. The commit exists only on your machine. Reset moves the current branch pointer back to an earlier commit, and its three modes decide what happens to the changes that commit contained.
git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
The notation HEAD~1 means one commit before the current one, so each line here undoes the most recent commit in a different way. The soft mode moves the branch back but leaves all the changes staged, ready to be committed again immediately; it is ideal for fixing a message or adding a forgotten file to the same commit. The mixed mode, which is the default if you give no flag, moves the branch back and leaves the changes in your working tree but unstaged, so you can re-stage selectively and split the work into better commits. The hard mode moves the branch back and deletes the changes from your working tree entirely, returning the files to the earlier commit state. Hard reset is the dangerous one in this family. Use it only when you are certain the discarded work has no value, because uncommitted deletions of that work cannot be brought back by restore. As a rule, soft and mixed resets are everyday tools, while hard reset deserves a pause and a status check before you press enter. Branch context matters here too: resetting while standing on the wrong branch compounds the problem, so confirm your branch first, a habit the branching workflow guide reinforces.
Undo a Shared Commit with revert
Everything changes once a commit has been pushed and teammates may have pulled it. Rewriting that history with reset and a force push replaces the shared past, and every teammate copy of the repository then disagrees with the remote, producing confused merges and lost work. Git provides revert for exactly this situation. Instead of removing the bad commit, revert creates a brand new commit that applies the opposite changes, cancelling the bad one out while leaving history intact.
git revert a1b2c3d git revert HEAD
The first line reverts a specific commit identified by its short hash, which you can copy from git log output. The second line reverts the most recent commit, since HEAD simply names the current one. In both cases Git creates the inverse commit for you and opens an editor for its message, which by default explains what was reverted. Because revert adds history rather than rewriting it, it is safe to push like any normal commit, and teammates pull it like any normal change. The bad commit remains visible in the log, which is a feature, not untidiness: the record honestly shows that a change was made and then deliberately undone, and if the undo itself turns out to be wrong, the revert can be reverted. For shared branches, revert is almost always the correct answer. Save reset for commits that have never left your machine.
Fix the Last Commit with amend
A smaller, friendlier undo covers the most frequent commit mistake of all: you committed a second too early and either forgot a file or mistyped the message. Rather than resetting, you can fold the correction into the same commit with the amend flag, provided the commit has not been pushed.
git add forgotten.css git commit --amend --no-edit git commit --amend -m "Add homepage layout and styles"
In the first pair of lines, you stage the forgotten file and then amend. The no-edit flag keeps the existing message while the commit is rebuilt to include the newly staged file, so the history shows one complete commit instead of a commit plus an embarrassing follow-up. The second example amends only the message, replacing it with the corrected text inside the quotes. Amend rewrites the commit, giving it a new identifier, which is exactly why the not-pushed condition matters. Amending a private commit is clean and routine. Amending a shared one creates the same history mismatch as a reset, and the fix becomes a force push that disrupts everyone. When in doubt, check whether the commit is already on the remote before amending.
Recover Lost Commits with reflog
Sooner or later someone runs a hard reset or deletes a branch and then realises the discarded commit held real work. This is where Git earns its safety net reputation. Git quietly keeps a log of everywhere HEAD has pointed, called the reflog, including commits that no branch currently reaches. As long as the commit existed, the reflog can usually find it.
git reflog git switch -c recovery a1b2c3d
The first command lists recent positions of HEAD, each with a short hash and a short description of the action that moved it, such as a commit, a reset, or a checkout. You scan the list for the state just before the mistake, note its hash, and then the second line creates a new branch pointing at that commit, bringing its files and history back into normal view where you can inspect, merge, or cherry-pick from them safely. Nothing is overwritten by this recovery, because you are adding a branch, not moving an existing one. The honest limit: reflog entries expire, typically after about ninety days, and work that was never committed at all, such as changes deleted from the working tree before any staging, generally cannot be recovered this way. The practical lesson is to commit early drafts rather than leaving valuable work uncommitted for days, a point the daily routine in the basic commands guide makes as well.
Undo a Merge and Other Awkward Cases
Merges deserve their own note because they combine many commits at once. If a merge is still in progress and Git is waiting with conflicts you do not want to resolve, you can abandon it cleanly and return to the exact state before the merge began.
git merge --abort
This single command cancels the in-progress merge and restores your files to their pre-merge state. It only works while the merge is unfinished; once a merge is committed, undoing it is a job for revert, using the special form that names the merge and its main parent line, or for reset if the merge commit is still private. Read the log carefully before choosing, because reverting a merge and later trying to merge the same branch again has subtleties that surprise even experienced developers. When a merge goes wrong in a shared repository, the calm procedure is to stop, note the hashes involved, and pick the undo with the team rather than improvising under pressure.
Two smaller cases round out the toolkit. To discard changes in only part of your work, restore accepts individual file names, so you can keep good edits and drop bad ones file by file instead of all or nothing. To temporarily shelve work rather than undo it, the stash command from the basics guide parks uncommitted changes and lets you reapply them later, which is an undo that expects to be redone. Knowing the difference between discarding, unstaging, resetting, reverting, and stashing is most of the skill; the commands themselves are short.
Choosing the Right Undo: A Quick Reference
When something has just gone wrong, run through three questions in order. First, was the mistake committed? If not, restore handles files and restore with the staged flag handles the staging area, and both leave history alone. Second, if it was committed, has it been pushed? If not, reset in soft or mixed mode rewinds it gently, amend fixes small slips, and hard reset removes it entirely when you are sure. Third, if it has been pushed and shared, revert it, adding an inverse commit that keeps everyone copy of history in agreement. If you have already rewound and fear the work is gone, check the reflog before panicking. Keep a terminal habit that supports all of this: status before and after any undo, so you see the state change you intended and nothing else. Server-side work follows similar caution, and our SSH cheat sheet is a useful companion when the repository lives on a machine you manage directly. Files outside Git, such as uploads and configuration handled through everyday Linux commands, have no such safety net, which is one more reason to keep everything that matters inside version control.
Conclusion
Git undo is not one command but a small family, each member matched to a stage of work. Restore discards or unstages uncommitted changes, reset rewinds private commits with soft, mixed, or hard force, revert safely cancels shared commits by adding an inverse one, amend polishes the latest commit, and reflog rescues commits you thought were gone. Ask whether the work was committed and whether it was shared, and the right choice follows. Work that way, and mistakes in Git become brief detours instead of disasters.
Tagged: git reflog git reset git restore git revert git undo version control



2 thoughts on “How to Undo Things in Git: Reset, Revert and Restore Explained”