Short answer: Match the command to the mistake. git stash puts unfinished work aside (git stash pop brings it back). git restore <file> throws away changes in a file. git reset --soft HEAD~1 undoes your last commit but keeps every change. git revert <commit> undoes an old commit with a new one, which is safe on shared branches. And git reflog lists everywhere you have been, so you can find a commit you thought was gone.
Git rarely loses anything, but its undo commands each do something different, and picking the wrong one is how a small mistake becomes a big one. This post is the table you want open when something just went wrong.
Checked against the Git reference docs on 14 November 2026.
Which command undoes which mistake?
| Mistake | Command | What happens |
|---|---|---|
| You need to switch tasks, but your work is half done | git stash | Saves your changes and gives you a clean working folder |
| You want that half-done work back | git stash pop | Puts the latest stashed changes back and removes them from the stash |
| You changed a file and want the last committed version | git restore <file> | The file goes back to its last committed state (or to what you staged with git add); your edits are gone |
| You committed too early, or with the wrong message | git reset --soft HEAD~1 | The commit disappears, every change stays staged, ready to commit again |
| A commit already pushed to a shared branch broke something | git revert <commit> | A new commit that reverses the old one; history stays intact |
| You reset or deleted a branch and lost a commit | git reflog | Lists where HEAD has been, with the commit ids to go back to |
Why use revert instead of reset on a shared branch?
reset rewrites history: the commit is gone from your branch. If others already pulled it, their history and yours no longer match, and the next push turns into a mess. revert adds a new commit that undoes the changes, so everyone simply pulls one more commit. Rule of thumb: reset for commits only you have, revert for anything pushed.
How do you get a "deleted" commit back?
Run git reflog. Each line is a place HEAD has been:
git reflog
# 3f2a9c1 HEAD@{0}: reset: moving to HEAD~1
# 8d41e07 HEAD@{1}: commit: add signup validationThe commit 8d41e07 is still there. Bring it back on a new branch:
git switch -c rescue 8d41e07Git keeps reflog entries for 90 days by default (the gc.reflogExpire setting), so even last month's mistake is usually still in there.
Which of these can lose work for good?
git restore <file> on changes you never committed or stashed: those edits were never saved in Git, so there is nothing to get back. Stash or commit first if you are unsure. Everything else in the table keeps your work somewhere you can find.
Where can you check this?
- Git: git stash, git restore, git reset and git revert
- Git: git reflog, including the 90-day default
- Pro Git, the free book: Undoing things
What comes after the rescue?
The other fifteen commands you will use every day, from init to rebase, are in the 20 Git commands you actually need, with its own undo table. And once your projects are on GitHub, Profile README Studio builds the README for your profile page from 31 layouts and 45 block types, with a preview that looks the way GitHub shows it.

