Undo a mistake in Git: 5 commands that save you

Broke something in Git? Five commands cover almost every rescue: git stash, git restore, git reset --soft, git revert and git reflog, which even finds a commit you thought you deleted.

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.

Nothing loads from YouTube until you press play. Then Google receives your IP address and may set its own storage. Narrated with an AI-generated voice. Privacy

Which command undoes which mistake?

MistakeCommandWhat happens
You need to switch tasks, but your work is half donegit stashSaves your changes and gives you a clean working folder
You want that half-done work backgit stash popPuts the latest stashed changes back and removes them from the stash
You changed a file and want the last committed versiongit 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 messagegit reset --soft HEAD~1The commit disappears, every change stays staged, ready to commit again
A commit already pushed to a shared branch broke somethinggit revert <commit>A new commit that reverses the old one; history stays intact
You reset or deleted a branch and lost a commitgit reflogLists 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 validation

The commit 8d41e07 is still there. Bring it back on a new branch:

git switch -c rescue 8d41e07

Git 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?

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.

The tool

Do it in your browser.

GitHub

Profile README Studio

Build a GitHub profile README with a pixel-accurate preview, and badges, icons and cards the studio draws itself instead of outside servers.

Keep reading

More from the blog.

Every post