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. To undo the last Git commit safely, choose the command that matches the state of that commit.
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 workflow focuses on last-commit mistakes 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
Many desktop and developer-focused Linux installations already include Git, while minimal systems may not. Your version number will vary by distribution, but these Git undo commands are available in current Git releases. 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
773158e Add dashboard feature c855955 Initial commit On branch main nothing to commit, working tree clean
In these examples, HEAD is the current commit, HEAD~1 is the parent of that commit, and HEAD~2 moves back two commits.
Undo the Last Git Commit with Git Commands
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 fa1b84b] Corrected commit message Date: Sat Feb 28 07:45:13 2026 +0800 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 draft.txt
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 68f70bd] Add dashboard feature Date: Sat Feb 28 07:45:13 2026 +0800 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 draft.txt create mode 100644 forgotten-file.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
Use reset methods only 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 c855955 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
[main 4a89d56] Revert "Add new feature" Date: Sat Feb 28 07:50:00 2026 +0800 1 file changed, 1 deletion(-)
Push the new revert commit after it is created:
git push origin main
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 Techniques to Undo Commits
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
0211f35 HEAD@{0}: commit (amend): Add feature
4d9f79b HEAD@{1}: commit: Add feature
3638e85 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 1e12748. (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.
Undo All Unpushed Git Commits on a Private Branch
If more than one local commit sits ahead of the remote branch, inspect the exact range before resetting. This keeps a broader Git reset workflow from deleting work you meant to keep.
git log origin/main..HEAD --oneline
6ca1e2c Add local note ef5c05c Add local change
git reset --hard origin/mainremoves every local commit shown in that range and discards tracked working-tree changes. Use it only when the branch is private and every listed commit should disappear.
When the listed commits are all disposable, reset the local branch back to the remote tracking branch:
git reset --hard origin/main
git status
HEAD is now at 40a3100 Initial commit On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
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 failures are the ones most likely to block a last-commit repair.
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. Use git update-ref -d HEAD to undo the initial Git commit while keeping the files staged for a new first commit:
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 When Local Changes Would Be Overwritten
If the same file has uncommitted edits, Git protects those edits and stops before starting the revert:
error: Your local changes to the following files would be overwritten by merge: app.txt Please commit your changes or stash them before you merge. Aborting fatal: revert failed
Check the dirty file before choosing a fix:
git status --short
M app.txt
Commit the local edit if it is ready, or stash it before rerunning the revert. Reapply the stash only if you still need those local edits after the revert succeeds, and expect to resolve conflicts if the saved edit touches the reverted lines.
git stash push -m "work before revert"
git revert HEAD --no-edit
# Reapply the stash only if you still need those local edits; resolve conflicts if prompted
git stash pop
Verify the fix with git log --oneline -n 3. A successful revert adds a new revert commit, and your saved local edit remains available through the stash until you pop or drop it.
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
40a3100 HEAD@{0}: reset: moving to HEAD~1
dfb4790 HEAD@{1}: commit: Important feature
40a3100 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 dfb4790 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.
Conclusion: Choose the Right Git Undo Command
A bad last Git commit no longer needs guesswork. You can amend small mistakes, reset unpushed history with the right safety level, or revert pushed work without breaking a shared branch. If the commit exposed files that should be ignored, clear Git cache before recommitting. If the mistake landed on the wrong line of work, switch Git branches first.


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>