Guides · git
How to undo the last git commit
By Lalla Asmaa Alaoui, founder of Mel · Updated
Run git reset --soft HEAD~1 to undo your last commit and keep every change staged, ready to commit again. If you already pushed that commit, use git revert HEAD instead, which adds a new commit that cancels it without rewriting shared history.
The right command depends on two questions: do you want to keep the changes, and has anyone else already seen the commit? The steps below cover each case. They work the same in Terminal on Mac, in a Linux shell, and in Command Prompt or PowerShell on Windows.
Before you start: check where you are
- Look at the last few commits.
git log --oneline -5The top line is the commit you are about to undo. Make sure it is the one you mean.
- Check whether it was pushed.
git statusIf git says your branch is "ahead of 'origin/main' by 1 commit", the commit exists only on your machine. If it says "up to date", it was pushed, so skip to step 6.
Undo a commit that is only on your machine
- Keep the changes, staged. This is the safest undo. The commit disappears, and its changes wait in the staging area.
git reset --soft HEAD~1Use this when you forgot a file, or want to change the commit message. Add what you need and commit again.
- Keep the changes, unstaged. This is git's default reset. The changes stay in your files but are no longer staged.
git reset HEAD~1Use this when you want to split the work into smaller commits with git add -p.
- Throw the changes away. This deletes the commit and its changes from your working files.
git reset --hard HEAD~1Only use --hard when you are sure. Any uncommitted work in your files is lost too, so run git status first.
Undo a commit you already pushed
- Revert it. A revert adds a new commit that does the opposite of the old one. History stays intact, so teammates who pulled the commit have no problems.
git revert HEAD
git pushGit opens your editor for the message. Save and close it to finish. Add --no-edit to accept the default message.
If you pushed to a branch only you use, you can reset and then force push with git push --force-with-lease. Never do this on a shared branch such as main.
Only want to fix the message or add a file?
- Amend instead of undoing. For a commit that is not pushed yet:
git add forgotten-file.txt
git commit --amend--amend replaces the last commit with a new one that includes your staged changes and lets you edit the message.
A note for Windows
HEAD~1 works in Command Prompt, PowerShell and Git Bash. Some guides write HEAD^ instead. In Command Prompt the ^ character is an escape character, so HEAD^ can fail there. Stick with HEAD~1 on Windows.
Made a mistake? Use the reflog
- Find the lost commit. Git keeps a record of where your branch pointed, even after a hard reset.
git reflogFind the line for the commit you want back, then return to it:
git reset --hard <commit-id>The reflog is kept on your machine for a limited time, so recover sooner rather than later.
Doing it with Mel
Mel runs your own shell, so you can type any of the commands above at the prompt, exactly as you would in another terminal. Each command and its output becomes a block you can select and copy.
You can also ask in plain English. Press ctrl+enter (cmd+enter on macOS) and write something like "undo my last commit but keep the changes". The agent can check git log and git status, then propose the command. With Mel's approvals, a command that changes your repo appears on an approval card, so you see git reset --soft HEAD~1 before it runs and can deny it.
Afterwards, open the code review panel with ctrl+shift+= (cmd+shift+= on macOS) to see every uncommitted change as a diff, including the work that just came out of the commit.
FAQ
How do I undo the last commit but keep my changes? Run git reset --soft HEAD~1. The commit is removed and the changes stay staged. Use git reset HEAD~1 if you want them unstaged instead.
How do I undo a commit that I already pushed? Run git revert HEAD, then git push. This adds a new commit that cancels the old one, which is safe on shared branches.
What is the difference between git reset and git revert? git reset moves your branch back and removes commits from its history. git revert keeps history and adds a commit that reverses the changes. Reset is for local work; revert is for pushed work.
Can I get a commit back after git reset --hard? Usually yes. Run git reflog, find the commit, and run git reset --hard <commit-id>. Uncommitted changes that were never in a commit cannot be recovered this way.
How do I undo the last two commits? Use HEAD~2 instead of HEAD~1, for example git reset --soft HEAD~2. For pushed commits, revert each one, newest first.
- How to see what changed before you commit
- How to discard local changes to a file in git
- How to review an AI agent's code changes
- What is a coding agent?
Facts checked on Oct 25, 2026.
Git is a trademark of its owner. Mel is not affiliated with or endorsed by Git.
Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.
Follow on LinkedIn