How to Undo a Commit in Git (The Right Way)
Everyone needs to undo a commit at some point. This guide covers every safe way to do it: whether the commit is local, already pushed, or deep in history.
Beginner10 min readUpdated
How do you undo a commit in Git?
Undoing a commit in Git means reversing or removing a commit from your history. The right tool depends on whether the commit is local or already pushed: git reset rewrites history (local only), git revert creates a new undo commit (safe for pushed), and git commit --amend fixes the last commit before pushing.
The short version
- 1Check if commit is pushed:
git log --oneline origin/main..HEAD - 2Local commit only:
git reset --soft HEAD~1(keep changes) or--hard(discard) - 3Pushed commit:
git revert HEADthengit push - 4Fix last commit message:
git commit --amend -m 'New message'(before pushing only) - 5Recover from accidental reset:
git reflogto find lost SHA
Why it matters
You just made a commit, and immediately realized it was wrong. Maybe you committed to the wrong branch, included a debug file, wrote a broken message, or pushed something that shouldn't have gone out. Now what?
Git gives you multiple ways to undo a commit, but each one works differently and has different consequences, especially if the commit was already pushed. Picking the wrong method can rewrite shared history and cause chaos for your team.
Where it usually goes wrong
- Using
git reset --hardon a pushed commit and force-pushing (rewrites shared history) - Using
git revertwhen you actually neededgit reset(adds an unnecessary commit) - Losing work by running
git reset --hardwithout understanding what it discards - Amending a pushed commit and then being confused by the diverged history
Undoing commits, step by step
Step 1: Understand your situation first
Before running any command, determine: is the commit local (not yet pushed) or already pushed to a shared remote? This is the single most important factor in choosing the right approach.
Terminal# Check if your commit has been pushedgit log --oneline origin/main..HEAD# If this shows commits → they are LOCAL only (safe to reset)# If this shows nothing → your commits are already on the remoteTip: When in doubt, use
git revert: it's always safe because it doesn't rewrite history.Step 2: Undo the last commit but keep your changes (soft reset)
Use
git reset --soft HEAD~1to undo the last commit while keeping all your changes staged. The files stay exactly as they were, only the commit is removed. Perfect for fixing a bad commit message or re-committing with different files.Terminal# Undo the last commit, keep changes stagedgit reset --soft HEAD~1# Your changes are still staged - edit and recommitgit commit -m "Better commit message"# To undo the last 3 commits (keep all changes staged)git reset --soft HEAD~3Tip:
--softis the safest reset: no work is lost. Think of it as 'uncommit but keep everything.'Step 3: Undo the last commit and unstage changes (mixed reset)
Use
git reset HEAD~1(mixed is the default) to undo the last commit and unstage the changes. The files are still in your working directory but no longer staged. Useful when you want to re-select which files to include.Terminal# Undo the last commit, keep changes in working directory (unstaged)git reset HEAD~1# Or explicitly:git reset --mixed HEAD~1# Now choose what to re-addgit add src/feature.tsgit commit -m "Add only the feature file"Step 4: Undo the last commit and discard all changes (hard reset)
Use
git reset --hard HEAD~1to completely remove the last commit AND discard all its changes from your working directory. This is destructive: the changes are gone.Terminal# DESTRUCTIVE: undo last commit AND discard all changesgit reset --hard HEAD~1# Go back 3 commits, discarding everythinggit reset --hard HEAD~3# Reset to a specific commit SHAgit reset --hard a3b2c1dCareful:
--hardpermanently discards your working directory changes. Only use it on commits that have NOT been pushed to a shared remote, and only when you're certain you don't need the changes.In Gitoryx: Right-click any commit in the visual graph to reset, revert, or amend it, with no commands to remember.How it works Step 5: Undo a pushed commit safely with git revert
When a commit has already been pushed and other developers might have pulled it, use
git revert. It creates a new commit that inverses the changes, without rewriting history. This is the only safe way to undo a pushed commit.Terminal# Revert the most recent commit (creates a new 'undo' commit)git revert HEAD# Revert a specific commit by SHAgit revert a3b2c1d# Revert without immediately committing (review first)git revert --no-commit a3b2c1dgit revert --continue# Revert a range of commits (exclusive of start)git revert HEAD~3..HEADTip: After
git revert, justgit push, no force push needed. The history is preserved with an additional revert commit on top.Step 6: Fix the last commit with --amend
If you just committed and want to fix the message or add a forgotten file,
git commit --amendreplaces the last commit entirely. Only use this before pushing.Terminal# Fix the commit message of the last commitgit commit --amend -m "Correct commit message"# Add a forgotten file to the last commitgit add forgotten-file.tsgit commit --amend --no-edit# Amend opens your editor to rewrite the messagegit commit --amendCareful: Never amend a commit that has already been pushed to a shared branch. Amend rewrites the commit SHA. Anyone who already pulled will have a diverged history.
Mistakes to avoid with undoing commits
- Force-pushing after a reset on a shared branch
- Running
git reseton a pushed branch and thengit push --forcerewrites the remote history. Any teammate who already pulled will now have a diverged branch and will need to manually reconcile. - Fix: Use
git revertinstead for pushed commits. If you absolutely must reset a shared branch, coordinate with the team first and usegit push --force-with-leasefor a slightly safer force push. - Using git reset --hard without realizing changes are gone
--harddiscards your working directory changes permanently. Unlike--softand--mixed, there's no way to get those changes back through normal Git commands.- Fix: Always prefer
--softor--mixedunless you're certain you want to throw away the changes. If you already ran--hard, trygit reflogto find the old commit SHA and recover it. - Amending a commit that was already pushed
- Amend rewrites the commit SHA. If you then push, Git rejects it (non-fast-forward). Force-pushing 'fixes' it on the remote but diverges anyone who pulled.
- Fix: Only amend local commits. If already pushed, use
git revertor add a follow-up commit instead.
Undoing commits in Gitoryx
Gitoryx lets you undo commits directly from the visual commit graph: right-click any commit to reset, revert, or amend it. The graph updates instantly so you can see exactly what changed.
- Right-click any commit in the visual graph to reset, revert, or amend it, with no commands to remember.
- Gitoryx warns you before a destructive reset if the commit has already been pushed.
- The commit graph updates immediately so you can see exactly how your history changes after an undo.
- Revert a commit from any point in history in one click: Gitoryx handles the SHA lookup automatically.
- Use the graph to visually confirm which commits are local vs pushed before choosing a reset strategy.
Free plan · no account required · macOS, Windows and Linux
Undoing commits: frequently asked questions
- What is the difference between git reset and git revert?
git resetremoves commits from the history by moving the branch pointer backward, which rewrites history.git revertcreates a new commit that undoes the changes of a previous commit, and history is preserved. Usegit resetfor local commits only; usegit revertfor commits that have been pushed.- How do I undo a commit that was already pushed?
- Use
git revert <commit-sha>to create a new commit that reverses the changes, then push normally. Never usegit reseton a pushed commit unless you're willing to force-push and coordinate with your team. - What does git reset --soft do?
git reset --soft HEAD~1undoes the last commit but keeps all changed files staged. Your work is not lost: the commit is simply removed and everything is ready to re-commit.- What does git reset --hard do?
git reset --hard HEAD~1undoes the last commit AND permanently discards all changes in your working directory and staging area. Use with caution: this cannot be undone through normal Git commands.- How do I fix a commit message after committing?
- If the commit has NOT been pushed yet, use
git commit --amend -m 'New message'. If it was already pushed, usegit revertand commit with the correct information, or coordinate with your team before force-pushing an amended commit. - How do I undo multiple commits?
- For local commits:
git reset --soft HEAD~3to undo the last 3 commits while keeping changes staged. For pushed commits: revert each one (git revert HEAD~2..HEAD) or use an interactive rebase on a private branch. - Can I recover a commit after git reset --hard?
- Yes, if you act quickly. Run
git reflogto find the SHA of the lost commit, thengit checkout <sha>orgit reset --hard <sha>to restore it. Git keeps unreferenced objects for ~30 days before garbage-collecting them. - Is git revert safe for shared branches?
- Yes, it's the recommended approach for shared branches.
git revertonly adds a new commit, it never modifies existing history. Anyone who has already pulled the original commit will be able to simply pull the revert commit.
Related reading
- How to Resolve Merge Conflicts in GitTutorial · 12 min
- Interactive Rebase Guide: Clean Up Your Git HistoryTutorial · 13 min
- Git Stash Tutorial: Save Work Without CommittingTutorial · 8 min
- How to Undo Almost Anything in GitBlog · 14 min
- Recover Lost Commits With git reflogBlog · 10 min