Fixes

How to Undo the Last Commit in Git (and Keep Your Changes)

By Admin · Updated · 5 min read · Checked against the official Git docs

To undo your last commit and keep the changes, run git reset --soft HEAD~1. The commit disappears, and everything it contained goes back to the staging area, ready to commit again. Nothing in your files changes.

Quick answer

Not pushed yet: git reset --soft HEAD~1. Already pushed to a shared branch: git revert HEAD. Just a typo in the message: git commit --amend.

Undo the last commit in Git with git reset --soft HEAD~1

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.

“Undo” can mean four different things here, and picking the wrong one is how people lose work. So start with this table, then jump to the section you need.

Which undo do you need?

You want to… Run Your changes
Undo the commit, keep changes staged git reset --soft HEAD~1 Kept, staged
Undo the commit, keep changes but unstaged git reset HEAD~1 Kept, unstaged
Throw the commit and its changes away git reset --keep HEAD~1 Deleted
Undo a commit that’s already pushed git revert HEAD Reversed by a new commit
Only fix the message or add a forgotten file git commit --amend Kept

HEAD~1 means “one commit before the current one”. Use HEAD~2 to undo the last two commits, and so on.

Undo the last commit and keep changes staged

This is the one most people want: you committed too early, or you want to split the commit, or you want to add one more file before committing again.

$ git log --oneline
195c478 Add cart and styles
705cd01 Add homepage
$ git reset --soft HEAD~1
$ git status --short
M  app.js
A  style.css
$ git log --oneline
705cd01 Add homepage

The M and A in the first column mean the changes are still staged (modified and added). The commit is gone from git log. Make your fixes and commit again.

Undo the commit and unstage the changes

Leave out --soft and Git uses --mixed, the default. The commit goes away, and your edits stay in your files but are no longer staged. Use this when you want to pick again which changes go into the next commit.

$ git reset HEAD~1
Unstaged changes after reset:
M	app.js
$ git status --short
 M app.js
?? style.css

Notice style.css now shows as ?? (untracked). That file was new in the commit you undid, so after a mixed reset Git no longer tracks it. It’s still on disk; git add it again when you’re ready.

Undo the commit and delete its changes

Most tutorials tell you to use git reset --hard HEAD~1 here. It works, but it also wipes any uncommitted work you had lying around. --keep is the safer choice: it throws away the commit and its changes, but refuses to run if it would overwrite something you haven’t committed.

$ git reset --keep HEAD~1
$ git status --short
?? notes.txt

The commit’s changes are gone, while notes.txt (an unrelated file we hadn’t committed) survived. With --hard it would have survived too, because it was untracked, but any edits to tracked files would not have.

Only reset commits you haven’t pushed. Reset rewrites history. If teammates already pulled the commit, use git revert instead (next section).

Undo a commit you already pushed

Once a commit is on a shared branch, don’t remove it. Add a new commit that reverses it:

$ git revert --no-edit HEAD
[main 7d54360] Revert "Add cart"
 Date: Wed Nov 18 08:42:00 2026 +0000
 1 file changed, 1 deletion(-)
$ git log --oneline
7d54360 Revert "Add cart"
683f9f9 Add cart
6b1e2a5 Add homepage

History now shows both the original commit and its reversal, which is exactly what you want on main: nobody’s clone breaks, and anyone reading the log can see what happened. Push it like any other commit. Our git revert guide covers reverting older commits, several commits at once, and merge commits.

If the branch is yours alone (a feature branch nobody else uses), you can reset and then force push with git push --force-with-lease. It refuses to overwrite the remote if someone pushed in the meantime, which plain --force does not.

Changed your mind? Get the commit back

Reset doesn’t delete commits straight away. Git records every place HEAD has pointed in the reflog, so you can jump back:

$ git reset --hard HEAD~1
HEAD is now at 4160bea Add homepage
$ git reflog -3
4160bea HEAD@{0}: reset: moving to HEAD~1
ae7d69c HEAD@{1}: commit: Add cart
4160bea HEAD@{2}: commit (initial): Add homepage
$ git reset --hard HEAD@{1}
HEAD is now at ae7d69c Add cart
$ git log --oneline -1
ae7d69c Add cart

HEAD@{1} means “where HEAD was one move ago”. Unreachable commits stay in the reflog for 30 days by default, so you have time. The one thing the reflog can’t restore is uncommitted work you deleted with --hard.

Common mistakes

  • Using HEAD^ in Windows CMD. In cmd.exe the caret is an escape character, so git reset HEAD^ can fail with “More?”. Use HEAD~1, which works in every shell.
  • Undoing the first commit of a repo. There’s no HEAD~1 before the first commit. Use git update-ref -d HEAD to remove it and keep the files staged.
  • Resetting after pushing, then pulling again. If you reset a pushed commit and run git pull, the commit comes right back. Either revert it, or reset and force push.

FAQ

How do I undo the last commit but keep the changes?

Run git reset --soft HEAD~1. The commit is removed and its changes stay staged. Use git reset HEAD~1 if you want them unstaged instead.

How do I undo a commit that is already pushed?

Run git revert HEAD and push. It adds a new commit that reverses the old one without rewriting shared history.

What is the difference between git reset –soft and –hard?

--soft removes the commit and keeps its changes staged. --hard removes the commit and deletes its changes, plus any uncommitted edits to tracked files. See our git reset guide for all three modes.

Can I undo a git reset?

Yes, for committed work. Run git reflog, find the commit, and reset back to it with git reset --hard HEAD@{1} (or the hash).

How do I undo the last 3 commits?

Use git reset --soft HEAD~3 to undo them and keep all their changes staged as one set.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands