DEV Community

Cover image for Git Undo Generator: the Exact Command for Your Mess
Mr Say Nothing
Mr Say Nothing

Posted on Originally published at mrsaynothing.dev

Git Undo Generator: the Exact Command for Your Mess

Originally published at mrsaynothing.dev.

A teammate once answered "git reset --hard" to every undo question the way other people answer "turn it off and on again". It worked twice. The third time took a Friday afternoon and a reflog lesson nobody enjoyed. The Undo Anything in Git hub on my site grew out of that week — and today it gets a proper tool.

The git undo generator is live: three questions in, one exact command out. No account, no upload — everything runs in your browser. Pick whether the commit was pushed, whether you keep the changes, and whether anyone else pulls the branch; it prints the right git reset, git revert or reflog rescue, with a warning where the command bites.

Why build a tool for four commands?

Because the search data says people want the answer, not the essay. Google Search Console shows my site ranking for "git reset last commit keep changes" at position 12.3 — 27 impressions in one week, zero clicks. The query is a cry for a command, and the searcher scrolls past anything that makes them read. A generator answers in one screen.

The same pattern repeats across every variant: "and keep changes", "but keep changes", "keep the file". People aren't researching version control theory. They have a mess, right now, and they want the shortest path out of it.

How the generator decides

Three questions, straight from the revert-vs-reset decision guide:

  1. Where is the commit? Unpushed, pushed, or lost entirely.
  2. What happens to the changes? Keep them staged, keep them unstaged, or discard them.
  3. Who else pulls this branch? The question that separates a clean rewrite from a team incident.

The shared-branch question is the whole tool. Everything else is convenience; that one is damage control.

Its answers, in full:

Command History Working tree Shared branch?
git reset --soft HEAD~1 moves back 1 changes kept, staged no — rewrite
git reset HEAD~1 moves back 1 changes kept, unstaged no — rewrite
git reset --hard HEAD~1 moves back 1 changes discarded no — destructive
git revert HEAD adds an undo commit untouched yes — the safe move
git reflog + git switch -c recovery <sha> nothing lost restored from reflog rescue after anything

The commands it prints

Every case the generator covers, in copy-paste form:

# committed, not pushed, keep the changes staged
git reset --soft HEAD~1

# committed, not pushed, keep the changes unstaged
git reset HEAD~1

# pushed, others pull this branch — the only polite undo
git revert --no-edit HEAD~1..HEAD
git push

# committed and lost — reflog knows for ~90 days
git reflog
git switch -c recovery <sha-from-the-list>
Enter fullscreen mode Exit fullscreen mode

The HEAD~1..HEAD range form reverts exactly the commits you name without recreating them one by one — worth knowing even if the tool types it for you.

Under the hood

The logic is a decision table, not magic. Three questions give eighteen combinations; most collapse into the same handful of commands, because "unpushed + discard" and "pushed + my own branch + discard" differ only in the force-with-lease tail. The page ships as one static route — no server, no analytics, no cookies — so it loads on a phone in a server room, which is where most git emergencies happen, and the copy button hands you the exact block including the warning comments.

What it deliberately doesn't do

No interactive rebase help — if you're reordering commits, you already know more than the tool teaches. No stash surgery; that has its own guide (


). And it refuses to guess: when the situation is "pushed to a shared branch", the only command it offers is revert, because the alternatives burn teams. The long-form guide on undoing a commit explains what each flag does to the reflog; the tool just refuses to let you pick the wrong one at speed.

The source is at github.com/mrsaynothing/git-undo — plain HTML and JavaScript, no build step, no dependencies. Fork it, restyle it, rip out my wording. The version on the site runs the same logic as a static SvelteKit page.

If it saves you one reflog session this year, it has paid for the afternoon it took. If it gives the wrong answer for a case you hit, that's what the repository issues are for.


This ships daily at mrsaynothing.dev — the full archive, every piece in 21 languages, zero missed days. New posts land in the newsletter the moment they ship: join it here. Code at GitHub.

Top comments (0)