Git undo keep changes: five verbs, one fear
Git undo keep changes has one answer: git reset --soft HEAD~1 steps the branch label back one commit and hands the work back staged. Pushed? git revert HEAD adds an inverse commit, rewrites nothing.
Git undo keep changes has a single answer: git reset --soft HEAD~1 steps the branch label back one commit and hands your work back staged. Undo and cancel are the same reset in different accents; revert is the odd one because it adds history instead of rewinding it. Pushed? Revert, never rewrite.
Every word people type, what git actually does, and whether the work survives:
| Verb people type | What git actually does | Keeps the work? | The command |
|---|---|---|---|
| "undo" | Branch label moves back one commit; the changes return staged | Yes — in the index | git reset --soft HEAD~1 |
| "cancel" | Default reset: label moves back, changes return but unstaged | Yes — in the working tree | git reset HEAD~1 |
| "revert" | A new commit is added that undoes the old one; history is never rewritten | Yes — as an inverse diff | git revert HEAD |
| "unstage" | Paths leave the index; file contents are untouched | Yes — nothing left the working tree | git restore --staged file.txt |
| "shelve it for now" | Work is boxed and put on a shelf you can pop later | Yes — on the stash stack | git stash push -m "wip" |
Every command above keeps the work unless its name is --hard.
git undo keep changes: the two commands
HEAD~1 means one commit before where HEAD points now. After either command your files are intact on disk — only the branch pointer moved. Check git status: staged with --soft, unstaged with the default.
# commit gone — changes come back staged git reset --soft HEAD~1 # changes come back to the working tree instead git reset HEAD~1 # forgotten file: fold it in without un-committing git add forgotten-file.txt git commit --amend --no-edit
--soft keeps everything staged; the diff returns to the index exactly as committed. Amend rewrites the last commit in place; soft undoes it and waits.
| Flag | Commit undone? | Changes on disk | Staged? | Typical use |
|---|---|---|---|---|
--soft | Yes | Conserved | Yes | Re-commit with minor fixes |
--mixed (default) | Yes | Conserved | No | Regroup changes, re-stage selectively |
--hard | Yes | Deleted | — | Throw the work away |
git revert | No — new commit | Conserved | — | Undo an already-pushed commit |
git reset --soft HEAD~1. Yes → git revert HEAD. Either way the work survives — the choice is only about whose copy of history stays true. And if the flag that scares you is --hard: it is never required to keep changes.the escape hatches
# before any hard reset — branches are free
git branch backup-before-reset
git reset --hard HEAD~1
# already ran it? reflog remembers ~90 days
git reflog
git reset --hard HEAD@{1}
# one file only — modern spelling of checkout -- file
git restore file.txt
git restore --source <sha> file.txt
# right commit, wrong branch — move it, don't undo it
git branch stash-commit
git reset --hard HEAD~1
git cherry-pick <hash> Reset moves a label; revert adds a commit; neither deletes objects. The genuinely unrecoverable set is narrow: untracked files deleted by git clean -f, and reflog entries past their clock. Amending a pushed commit rewrites history — on a shared branch, revert instead. A bare --force overwrites whatever appeared; --force-with-lease refuses when the remote moved since your last fetch.
The word is the command
- Not pushed, fix and re-commit →
git reset --soft HEAD~1 - Not pushed, re-stage selectively →
git reset HEAD~1(mixed) - Want the changes gone →
git reset --hard HEAD~1(the reflog knows) - Already pushed to a shared branch →
git revert HEAD - The commit belongs on another branch →
git cherry-pick, don't undo
Revert never touches the past; reset rewrites it. If a bad commit really reached a server, check the machine's logs, not Git — on any systemd box, journalctl -u <service> -n 100 shows what ran and when.
- verb family: git 2.30+ semantics · recorded 2026-10-11 · source: official git docs
- flag table, reflog window and force-with-lease: git 2.4x, Linux · macOS · Windows · first published 2026-09-03, refreshed 2026-10-09
- reflog keeps HEAD's old positions ~90 days by default; every command here keeps the work unless its name is --hard