Undo Last Commit in Git: Keep Changes or Remove Commit Safely (2026 Guide)

Published February 16, 2026 · 9 min read

If you just made a bad commit, the fix depends on one question: do you want to keep your file changes? In most cases you do, and the right command is git reset --soft HEAD~1 or git reset HEAD~1.

This guide gives exact copy/paste commands for all common cases: keep changes staged, keep changes unstaged, discard everything, and safely undo a commit that is already pushed.

⚙ Quick links: Undo Pushed Commit (quick workflow) · Git Revert (pushed commits) · Git Reset (soft/mixed/hard/keep) · Git Undo Decision Guide · Git Reflog Recovery · Revert Merge Commit · Undo git add · Git Commands Cheat Sheet

Table of contents

  1. Copy/paste commands
  2. Which command to use
  3. Local commit workflows
  4. Already pushed commit workflows
  5. Recovery with reflog
  6. FAQ

1. Copy/paste commands

Undo last commit and keep changes staged (most common)

git reset --soft HEAD~1

Undo last commit and keep changes unstaged

git reset HEAD~1
# same as: git reset --mixed HEAD~1

Undo last commit and discard all tracked changes (destructive)

git reset --hard HEAD~1

Undo last pushed commit safely on shared branch

git revert HEAD
git push origin <branch>

Undo last 3 commits and keep everything staged

git reset --soft HEAD~3
Tip: Run git status before and after undo commands. It confirms whether changes are staged, unstaged, or discarded.

2. Which command should you use?

Your goal Command Result
Remove commit, keep all changes staged git reset --soft HEAD~1 Commit removed, files still staged
Remove commit, keep changes but unstage git reset HEAD~1 Commit removed, files unstaged
Remove commit and throw away tracked edits git reset --hard HEAD~1 Commit and tracked changes deleted
Undo pushed commit without rewriting history git revert HEAD New inverse commit is created
Warning: git reset --hard is destructive. If you are not 100% sure, use --soft or --mixed first, or stash changes before hard reset.

3. Local commit workflows

Case A: Commit message is wrong

Use soft reset, then recommit with a corrected message:

git reset --soft HEAD~1
git commit -m "Correct message"

Case B: You committed too early and want to edit files

Use mixed reset to unstage and keep files editable:

git reset HEAD~1
# edit files
# then stage only what you want
git add -p
git commit -m "Better scoped commit"

Case C: Split one bad commit into two clean commits

git reset HEAD~1
git add -p
git commit -m "Part 1"
git add -p
git commit -m "Part 2"

If you frequently do this, the deeper command behavior is covered in Git Reset: Soft, Mixed, Hard & Keep.

4. Already pushed commit workflows

On shared branches, prefer git revert because it does not rewrite public history:

# revert latest commit
git revert HEAD
git push origin main

# revert a specific commit
git revert <commit-hash>
git push origin main

Use force-push workflows only on private branches where nobody else depends on that history.

5. Recovery if you used the wrong command

If you accidentally ran git reset --hard and lost a commit, check reflog immediately:

git reflog
# find the commit before the reset, then:
git reset --hard <that-hash>

Reflog usually recovers committed history. It cannot recover uncommitted edits that were never recorded.

6. FAQ

Can I undo the last commit without changing files at all?

Yes. git reset --soft HEAD~1 removes the commit while preserving your exact file state and staging state.

What is the difference between HEAD^ and HEAD~1?

For normal commits they are equivalent. For merge commits, HEAD^ refers to the first parent and HEAD~1 follows the first-parent chain by one step.

Can I undo only the latest pushed merge commit?

Yes, but use git revert -m 1 <merge-hash>. See Git Revert a Merge Commit for the exact parent-selection workflow.

Where do I learn full reset vs revert vs restore decisions?

Use Git Undo: Reset, Revert & Restore for the full decision map.

How do I undo git add after resetting?

Run git restore --staged <file> or git restore --staged .. Detailed examples are in Undo git add.

Related Resources

Undo Pushed Commit Guide Use git revert safely when the bad commit is already pushed. Git Revert Complete Guide Undo pushed commits safely with practical workflows and conflict handling. Git Reset Complete Guide Deep dive on --soft, --mixed, --hard, --keep, and remote reset workflows. Git Undo Decision Guide Choose between reset, revert, restore, and reflog for each undo scenario. Git Reflog Recovery Guide Recover commits after bad reset, rebase, amend, or deleted branches. Git Commands Cheat Sheet Fast reference for daily Git commands and flags.