Git School · FluxGit

Undo the last Git commit: soft reset or revert

FluxGit ·

These blocks describe different cases, not one script. Choose the case that matches your repository. Replace example branch names, file paths and VERIFIED_COMMIT before use. Abort commands apply only to their active operation; do not run both alternatives.

Git School · G5

Try it in your browser

Your last commit should not exist. One is only on your machine; an older one is already pushed.

Goal: Undo the local commit, then undo the pushed one without rewriting shared history.

Practice uses a simulated repository in your browser memory. It does not access your files or connect to a real remote repository.

Commands to practise

This simulator supports a subset of Git commands. Unsupported commands explain their limits in the terminal.

git reset --soft HEAD~1
git restore --staged app.js
git restore app.js
git revert HEAD --no-edit
git push

With FluxGit

  • The Undo button reverses your last action, including a commit.
  • On a pushed commit, Revert commit makes the opposite commit for you, after a preview.
Download FluxGit Git School

Short answer

To undo a local last commit while keeping its changes, reset --soft moves the branch to its parent and leaves the index and working tree in place. To undo a commit already shared, revert adds a new inverse commit. Decide whether you want to keep the changes and whether other people have the history before choosing a command.

The example and its prerequisites

This example uses one ordinary non-merge commit with a parent, at the current branch tip. The working tree starts clean and no unrelated files are staged. A repository's first commit has no HEAD^ parent, and a merge has multiple parents; neither fits this exact shortcut. Inspect the graph rather than using HEAD~1 as an unexplained universal undo button.

Inspect before changing anything

Read the last commit's patch, status and branch sharing state. You may want to remove the commit wrapper while continuing its code, reverse code that reached a shared branch, or discard the code entirely. Those are different outcomes. Check with collaborators before rewriting a published branch. A recent timestamp or your own author name does not establish that a commit is private.

git status --short
git log -3 --oneline --decorate
git show --stat HEAD

Keep a reference or a separate checkout

Create rescue/before-undo at HEAD first. It keeps the original committed state reachable for review. Handle any uncommitted files separately; a branch reference does not store them. In this clean example, the commit's patch is the only work that reset --soft exposes as staged. With an already dirty index, the resulting staged diff can combine several tasks and needs separate review.

git branch rescue/before-undo HEAD
SCHOOL_UNDO=$(git rev-parse HEAD)

The first workflow

For the local keep-changes case, use reset --soft HEAD^ and inspect git diff --cached. The original commit's content remains in the index and working tree, ready for a revised commit. If you want to edit it unstaged, a mixed reset has different index behavior. Do not switch to --hard merely to make the staged status shorter: hard reset changes working files as well.

git reset --soft HEAD^
git diff --cached
git status --short

Choose the other outcome deliberately

For a shared ordinary commit, save its verified ID as SCHOOL_UNDO and use git revert on that ID. Revert creates another commit whose patch reverses the selected change, retaining the history colleagues already have. Review the result and project checks before an ordinary push. Reverting a merge requires selecting a mainline parent and understanding its later merge effects; use a dedicated procedure for that case.

git revert "$SCHOOL_UNDO"

Limits and exceptions

Revert can conflict when later work overlaps the selected change. Read status, resolve and stage the intended files, then use revert --continue. Use revert --abort if the selected change or outcome was wrong. Reset --soft does not have a matching continue/abort state. Avoid mixing the two workflows or starting another history repair while an active revert remains unresolved.

git status
git revert --abort

Conflicts and cleanup

If you truly intend to discard committed and local file changes, that is an additional destructive decision, not this guide's default. Preserve and inspect the available state first. Uncommitted work is not in reflog. With restore points enabled and local changes detected, FluxGit can save a safety stash before a supported hard reset; this plain terminal sequence does not invoke that protected app path.

Verify the result

After a soft reset, check the new branch tip and the staged patch against the rescue reference. After a revert, check that the new inverse commit affects only the intended behavior and leaves later wanted work intact. Run the relevant project checks. Keep the rescue branch during review and do not infer correctness just because Git completed the command without an error.

git log -4 --oneline --decorate
git status --short
git diff --cached

Common questions

Will soft reset delete my files? It preserves the working tree and index, while moving the branch reference. Does revert erase the old commit? No, it records a new inverse change. Can reflog recover everything? No; it concerns retained local reference states and committed objects, not arbitrary unsaved file contents.

Review the operation in FluxGit

Use FluxGit’s visual history and patch view to review the operation and the relevant commits. Keep the branch, sharing and local-file checks in this example. The download page lists the currently available builds and platforms.

Official Git references

Git School · 1:49

Undo a Git commit: before you push (reset) and after (revert)

Two commits you regret. One is already on the server. The other is only on your machine. They are undone differently.

Undo a Git commit: before you push (reset) and after (revert)
English narration. Manual subtitles are available on YouTube in 11 languages. This transcript is in English.

Watch on YouTube — opens a new tab

Recorded in a test repository.

Read the narration transcript
  1. 00:03 Two commits you regret. One is already on the server. The other is only on your machine. They are undone differently.
  2. 00:12 Here they are. Print totals is only here. Turn on debug logging is already pushed.
  3. 00:20 The rule is simple. A commit that is only on your machine can be taken back: move the branch back, nobody else has it. A commit that is already pushed is shared: never rewrite it. Instead, add a new commit that undoes it. That is a revert.
  4. 00:40 Not pushed: right-click the commit below it, soft reset current branch here. Low risk, with a restore point first.
  5. 00:50 The commit is gone, and its change is still staged. Nothing is lost.
  6. 01:00 Already pushed: revert commit. A new commit that undoes it, and the history everyone has stays as it was.
  7. 01:12 There it is: a revert commit, ready to push like any other.
  8. 01:20 Changed your mind? Both leave a restore point in the Safety Timeline.
  9. 01:26 In the terminal: git reset dash dash soft HEAD tilde one for a local commit; git revert and the commit for a pushed one, then push.
  10. 01:36 Recap. Only yours: reset. Shared: revert. Next lesson: clean up your commits before the pull request.

Related workflows

Download FluxGit