How do I undo my last commit but keep the changes?
Run git reset --soft HEAD~1. The commit is removed from the branch and its changes stay staged. If it was already pushed, use git revert instead.
Undo, recover, and fix, with the danger spelled out.
Type what you want to do, such as "undo my last commit" or "recover a deleted branch", and get the exact git command, what it does, how to undo it, and what changes if you already pushed. Forty-two scenarios, an undo decision tree, and the reflog safety net.
Loading the toolbox.
You do not have to take our word that your data stays here. Developer tools show every request a page makes, so you can watch this page work and confirm that nothing is sent. The check takes under a minute.
F12, or Cmd+Option+I on a Mac, and select the Network tab.Only two things can add a line to that log, and neither carries your data. Submitting the feedback form sends the vote and comment you typed. Typing characters in a script the page has not displayed yet, such as Cyrillic or Greek, can make the browser fetch the matching font file, and that request names only the font.
The production site also sends a Content-Security-Policy header that tells your browser to refuse connections to anywhere except this site and the anonymous analytics endpoint, so a coding mistake could not quietly send your data elsewhere.
Git has a command for almost every mistake, but the right one depends on details that are easy to get wrong in a hurry: whether the change was committed, whether it was pushed, and whether you want to keep the edits. This finder starts from what you want to do, written in plain words, and answers with the command, a line explaining each part, how to reverse it, and what to do differently when the commit is already on a shared branch.
It covers forty-two scenarios in eight groups: undoing commits and changes, rewriting commits, branches, working with the remote, stashes, files, reading history, and recovering lost work. The commands use the modern porcelain introduced in Git 2.23, git switch and git restore, instead of the overloaded git checkout, so each command does one thing.
Every scenario carries one of three badges, decided by what the command can lose rather than by how unfamiliar it looks.
git revert, git bisect, and git reflog are safe.git reset --hard, git restore on edited files, git clean -fd, and force pushes are destructive, and each shows a red warning that says exactly what cannot come back.The key fact behind the badges is that git only protects what was committed. Edits sitting in your working tree were never recorded anywhere, so a command that overwrites them has no undo.
It depends on one question above all: has anyone else got the commit? If it is only in your clone, you can rewrite it. git reset --soft HEAD~1 takes back the last commit and keeps its changes staged, git commit --amend fixes its message or adds a forgotten file, and an interactive rebase edits, squashes, or drops older commits.
Once a commit is pushed to a shared branch, rewriting it forces everyone else to repair their clones. The finder sends every pushed case to git revert, which adds a new commit that cancels the old one and needs no force push. A merge commit needs git revert -m 1 so git knows which parent to keep. If you truly must rewrite a shared branch, use git push --force-with-lease, which refuses to overwrite commits you have not seen.
When you are not sure which scenario fits, the decision tree asks short yes or no questions and stops as soon as it can recommend one. It asks at most five:
The answer is the same scenario card you would find by searching, with its command, its badge, and its undo note.
The reflog is a local diary of every position HEAD and each branch has had. Every commit, reset, rebase, merge, amend, and switch adds an entry, so after almost any accident you can run git reflog, find the line from before the mistake, and get back to it. The safest move is to name that state first with git branch rescue HEAD@{2}, inspect it, and only then reset to it.
The reflog has limits worth knowing. It lives only in your clone and is never pushed. It records commits, not uncommitted edits. And its entries expire: by git's documented defaults, gc.reflogExpire keeps reachable entries for 90 days and gc.reflogExpireUnreachable keeps unreachable ones for 30 days, after which garbage collection may prune the objects. Dropped stashes and deleted branches can also be found with git fsck until then.
No. It is a reference that runs entirely in your browser. It never runs git, never reads your files, and sends nothing anywhere; the commands are text for you to copy into your own terminal. Replace placeholders such as <sha>, feature-x, and main with your own names before running them. Other tools in the Commands lane build tar, ffmpeg, OpenSSL, and Dockerfile commands the same way.
Elsewhere in the toolbox: UUID, .gitignore, CSP, .htaccess, and more on the 2toolbox home page. Part of the 2suite.io privacy-first ecosystem.
Run git reset --soft HEAD~1. The commit is removed from the branch and its changes stay staged. If it was already pushed, use git revert instead.
Usually, yes. Find its last commit in git reflog and run git branch with the old name and that commit id. The commits stay until garbage collection prunes them.
Committed work is: find the earlier position in git reflog and reset back to it. Uncommitted edits that the reset overwrote are not recoverable from git.
Plain --force overwrites the remote branch no matter what. --force-with-lease refuses if someone pushed commits you have not fetched, so you cannot silently delete their work.
Since Git 2.23 they split the old git checkout into two clear commands, one for files and one for branches, which makes mistakes less likely.