Everyone makes the same Git mistake sooner or later: you commit, stare at the terminal for two seconds, and realize the message is wrong, a file is missing, or the whole thing should have stayed local. The fix is usually quick, but only if you choose the command that matches the problem.
Git splits these repairs into separate tools. git commit --amend rewrites the most recent unpushed commit, git reset removes a local commit while keeping or discarding changes depending on the mode, and git revert backs out a pushed commit without rewriting shared history. The sections below stay focused on the last-commit situations that waste the most time, including keeping changes, keeping them staged, undoing an amend, and recovering after a bad hard reset.
Understand How to Undo the Last Git Commit
To undo the last Git commit quickly, use git commit --amend to fix the latest unpushed commit, git reset --soft HEAD~1 to remove the commit but keep changes staged, git reset HEAD~1 to keep changes unstaged, git reset --hard HEAD~1 to discard tracked changes, or git revert HEAD to undo a commit that is already pushed.
Quick Reference for Git Undo Tasks
| Situation | Best Command | What Happens | Safe After Push? |
|---|---|---|---|
| Fix the last commit message or add a forgotten file | git commit --amend | Replaces the last commit with a new one | No |
| Undo the last commit but keep changes staged | git reset --soft HEAD~1 | Removes the commit and keeps everything staged | No |
| Undo the last commit but keep changes in your working tree | git reset HEAD~1 | Removes the commit and unstages the changes | No |
| Delete the last commit and discard tracked changes | git reset --hard HEAD~1 | Removes the commit and resets tracked files | No |
| Undo the last pushed commit on a shared branch | git revert HEAD | Creates a new commit that reverses the old one | Yes |
| Revert the last pushed commit but review the reverse changes first | git revert --no-commit HEAD | Applies the revert without creating the commit yet | Yes |
The most important decision is whether the commit is already pushed. If other people may have pulled it, do not rewrite shared history with git reset unless you have explicit agreement to force-push. On shared branches, git revert is the safer choice because it preserves the visible history.
Verify the Git Command Is Available
Check that Git is installed before you start undoing commits:
git --version
git version 2.x.x
Your version number will vary by distribution, but the undo commands in this guide are available in current Git releases across the major Linux distributions. If git --version returns command not found, use Install Git on Ubuntu, Install Git on Debian, Install Git on Fedora, or Install Git on Linux Mint.
Inspect the Last Git Commit Before You Undo It
Before changing history, confirm which commit is at HEAD and whether your working tree is already dirty. That avoids undoing the wrong commit or mixing unrelated changes into the recovery.
git log --oneline -n 3
git status
76d5c7f (HEAD -> main) Add dashboard feature c54e198 Initial commit On branch main nothing to commit, working tree clean
In the examples below, HEAD is the current commit, HEAD~1 is the parent of that commit, and HEAD~2 moves back two commits.
Use Git Commands to Undo the Last Commit
Change Only the Last Git Commit Message
If the code is fine and only the message is wrong, amend the last commit with a corrected message:
git commit --amend -m "Corrected commit message"
[main eea6696] Corrected commit message Date: Sat Feb 28 07:48:25 2026 +0800 1 file changed, 1 insertion(+) create mode 100644 readme.md
Git writes a new commit object with the updated message, so the commit hash changes. Use this only before you push the commit to a shared remote.
Add a Forgotten File to the Last Git Commit
If you forgot to stage one file, add it and amend the commit without changing the message:
git add forgotten-file.txt
git commit --amend --no-edit
[main 76d5c7f] Add dashboard feature Date: Sat Feb 28 07:45:13 2026 +0800 2 files changed, 2 insertions(+) create mode 100644 draft.txt
The --no-edit flag keeps the existing commit message. This is one of the fastest ways to undo a last commit mistake without losing work.
Amend rewrites history by replacing the old commit with a new one. If the commit is already pushed to a shared branch, use
git revertinstead ofgit commit --amend.
Undo the Last Unpushed Git Commit but Keep Changes Staged
The next three reset methods are for commits you have not pushed yet. Start with a soft reset when you want to remove the commit but keep everything staged for the next commit.
git reset --soft HEAD~1
git status
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: draft.txt modified: notes.txt
Your files stay exactly as they were in the last commit, and the changes remain staged. This is useful when you want to split the commit, add another file, or rewrite the message before committing again.
Undo the Last Unpushed Git Commit but Keep Changes Unstaged
The default git reset mode is mixed reset. It removes the last commit and leaves the changes in your working tree so you can review them before staging again.
git reset HEAD~1
Unstaged changes after reset: M notes.txt
git status
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: notes.txt Untracked files: (use "git add <file>..." to include in what will be committed) draft.txt no changes added to commit (use "git add" and/or "git commit -a")
This gives you a clean review point before you decide what to stage again. It is also identical to git reset --mixed HEAD~1.
Undo the Last Unpushed Git Commit and Discard Tracked Changes
git reset --hardpermanently discards tracked changes in the commit and your working tree. If you are not completely sure, stop atgit reset --softorgit resetinstead.
Use a hard reset only when you want the branch and tracked files to return to the previous commit exactly:
git reset --hard HEAD~1
git status
HEAD is now at c54e198 Initial commit On branch main nothing to commit, working tree clean
Use this only when the commit is local and you truly want it gone. Untracked files are not removed by git reset --hard, so only tracked content from the branch history is discarded here.
Undo the Last Pushed Git Commit Safely
If the commit is already on a shared branch, revert it instead of resetting history. This creates a new commit that applies the inverse change and keeps the remote history intact.
git revert HEAD --no-edit
git push origin main
[main d0a5b35] Revert "Add new feature" Date: Sat Feb 28 07:45:14 2026 +0800 1 file changed, 1 deletion(-)
After the push, teammates can pull normally without dealing with rewritten history. On shared branches, this is the safest way to undo the last pushed commit.
If you want to inspect the reverse changes before Git creates the revert commit, use git revert --no-commit HEAD. That applies the undo to your index and working tree first so you can review or combine it with other fixes.
Use Advanced Git Undo Techniques
Undo the Last Two Git Commits but Keep the Changes
When the last two commits need to be rewritten together, move HEAD back two commits with a soft reset:
git reset --soft HEAD~2
git status
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: app.txt new file: notes.txt
This is the cleanest way to undo the last two commits without losing the work. If you prefer the changes unstaged instead, use git reset HEAD~2.
Undo an Amended Git Commit
If you used git commit --amend and want the pre-amend commit back, Git’s reflog is the fastest recovery tool. It records where HEAD pointed before and after the amend.
git reflog -n 4
06141ff HEAD@{0}: commit (amend): Add feature
f4dcc1f HEAD@{1}: commit: Add feature
83018e7 HEAD@{2}: commit (initial): Initial commit
The entry at HEAD@{1} is the commit before the amend. A soft reset restores that commit while keeping the extra amended changes staged:
git reset --soft HEAD@{1}
git status
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: notes.txt
This is a safer recovery than a hard reset because the pre-amend commit is back, but the amended difference is still available for review.
Revert the Last Git Commit Without Creating the Commit Yet
Sometimes you want the safety of git revert but still want to inspect the reverse patch before writing a new commit. That is what --no-commit is for.
git revert --no-commit HEAD
git status
On branch main You are currently reverting commit 7505309. (all conflicts fixed: run "git revert --continue") (use "git revert --skip" to skip this patch) (use "git revert --abort" to cancel the revert operation) Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: app.txt
The reverse change is now staged, but Git has not created the revert commit yet. Finish with git commit -m "Revert ..." when the result looks right, or cancel the whole operation with git revert --abort.
Remove the Last Pushed Git Commit from a Private Branch
Only rewrite remote history on a private branch or after your team agrees to it. For shared or protected branches, use
git revertinstead.
If the branch is private and you need the last pushed commit removed from the remote history completely, reset locally and force-push with lease protection:
git reset --hard HEAD~1
git push --force-with-lease origin main
--force-with-lease is safer than plain --force because Git refuses the push if the remote branch moved in the meantime. Use this for accidental secret commits, broken generated artifacts, or any other last commit that must disappear from a private branch history.
Troubleshoot Common Git Undo Errors
Git undo commands are straightforward once the repository state is clear, but a few edge cases show up often. These are the failures most likely to block the last-commit workflows above.
Fix the HEAD~1 Ambiguous Argument Error in Git
If you see this when undoing the last commit:
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
The repository only has one commit, so HEAD~1 has no parent to point to. If you really want to remove that first commit and keep the files staged, delete the branch reference instead:
git update-ref -d HEAD
git status
On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: first.txt
That returns the repository to a “no commits yet” state while leaving the files staged for a new initial commit.
Fix Git Revert Conflicts on the Last Commit
If the files changed again after the commit you are trying to revert, Git may stop with a conflict:
error: could not revert f94cf66... Add new feature hint: After resolving the conflicts, mark them with hint: "git add/rm <pathspec>", then run hint: "git revert --continue".
Check the repository state to confirm Git is waiting for you to resolve the conflicted file:
git status
On branch main You are currently reverting commit f94cf66. (fix conflicts and run "git revert --continue") (use "git revert --skip" to skip this patch) (use "git revert --abort" to cancel the revert operation) Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution) both modified: app.txt
Resolve the file, stage it, and continue the revert. If the revert is the wrong path, abort it instead:
git add app.txt
git revert --continue
# Or cancel the entire revert
git revert --abort
Verify the fix with git log --oneline -n 3. A successful revert adds a new revert commit; an aborted revert leaves history unchanged.
Recover a Git Commit After a Hard Reset
If you reset too far with git reset --hard, the reflog usually gives you a recovery path:
git reflog -n 3
31b2914 HEAD@{0}: reset: moving to HEAD~1
aa71e29 HEAD@{1}: commit: Important feature
31b2914 HEAD@{2}: commit (initial): Initial commit
The lost commit is still visible at HEAD@{1}. Reset back to that entry:
git reset --hard HEAD@{1}
HEAD is now at aa71e29 Important feature
Run git log --oneline -n 3 afterward and confirm the recovered commit is back at HEAD. Reflog entries expire over time, so recover as soon as you notice the mistake.
For the full option sets behind these workflows, Git’s official manuals for git-commit, git-reset, git-revert, and git-reflog document the underlying flags in detail.
Git Undo Commit FAQ
Use git reset HEAD~1. That removes the last commit and leaves the file changes in your working tree. If you want those changes to stay staged instead, use git reset --soft HEAD~1.
Use git revert HEAD and push the new revert commit. Revert keeps the shared branch history intact, so other people can pull normally without dealing with rewritten history.
Yes. Run git reflog, find the entry from before the amend, and reset to it. A soft reset such as git reset --soft HEAD@{1} is usually the safest recovery because it restores the earlier commit while keeping the amended difference staged.
git reset moves the branch pointer and can rewrite history, which makes it best for local commits you have not shared. git revert creates a new commit that reverses an earlier one, which makes it safer for pushed commits on shared branches.
For a local commit, use git reset --hard HEAD~1 to remove the commit and discard tracked changes. If the commit is already pushed, only remove it from remote history on a private branch and use git push --force-with-lease after the reset.
Use git revert --no-commit HEAD. Git applies the reverse change to your index and working tree but does not create the new revert commit until you run git commit. If you change your mind, cancel it with git revert --abort.
Conclusion: Choose the Right Git Undo Command
A bad last Git commit no longer needs guesswork. You can amend it when the mistake is small, reset it locally with the right amount of cleanup, or revert it safely after push without breaking everyone else’s branch. If the commit also exposed files that should be ignored, clear Git cache next. If the fix belongs on a renamed line of work, rename a Git branch afterward.
Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>