Back
GitoryxGitoryx

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

On this page

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

  1. 1Check if commit is pushed: git log --oneline origin/main..HEAD
  2. 2Local commit only: git reset --soft HEAD~1 (keep changes) or --hard (discard)
  3. 3Pushed commit: git revert HEAD then git push
  4. 4Fix last commit message: git commit --amend -m 'New message' (before pushing only)
  5. 5Recover from accidental reset: git reflog to 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 --hard on a pushed commit and force-pushing (rewrites shared history)
  • Using git revert when you actually needed git reset (adds an unnecessary commit)
  • Losing work by running git reset --hard without understanding what it discards
  • Amending a pushed commit and then being confused by the diverged history

Undoing commits, step by step

  1. 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 remote

    Tip: When in doubt, use git revert: it's always safe because it doesn't rewrite history.

  2. Step 2: Undo the last commit but keep your changes (soft reset)

    Use git reset --soft HEAD~1 to 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~3

    Tip: --soft is the safest reset: no work is lost. Think of it as 'uncommit but keep everything.'

  3. 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"
  4. Step 4: Undo the last commit and discard all changes (hard reset)

    Use git reset --hard HEAD~1 to 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 a3b2c1d

    Careful: --hard permanently 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
  5. 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..HEAD

    Tip: After git revert, just git push, no force push needed. The history is preserved with an additional revert commit on top.

  6. 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 --amend replaces 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 --amend

    Careful: 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 reset on a pushed branch and then git push --force rewrites the remote history. Any teammate who already pulled will now have a diverged branch and will need to manually reconcile.
Fix: Use git revert instead for pushed commits. If you absolutely must reset a shared branch, coordinate with the team first and use git push --force-with-lease for a slightly safer force push.
Using git reset --hard without realizing changes are gone
--hard discards your working directory changes permanently. Unlike --soft and --mixed, there's no way to get those changes back through normal Git commands.
Fix: Always prefer --soft or --mixed unless you're certain you want to throw away the changes. If you already ran --hard, try git reflog to 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 revert or 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 reset removes commits from the history by moving the branch pointer backward, which rewrites history. git revert creates a new commit that undoes the changes of a previous commit, and history is preserved. Use git reset for local commits only; use git revert for 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 use git reset on 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~1 undoes 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~1 undoes 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, use git revert and 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~3 to 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 reflog to find the SHA of the lost commit, then git checkout <sha> or git 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 revert only 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.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64