Back
GitoryxGitoryx
Beginner10 min read

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.

The Problem

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.

Common mistakes developers make with this:

  • 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

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.

What is Undo a Commit in Git (The Right Way)?

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.

Step-by-Step Guide

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.

bash
# Check if your commit has been pushed
git 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

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

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.

bash
# Undo the last commit, keep changes staged
git reset --soft HEAD~1

# Your changes are still staged — edit and recommit
git commit -m "Better commit message"

# To undo the last 3 commits (keep all changes staged)
git reset --soft HEAD~3

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

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.

bash
# 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-add
git add src/feature.ts
git commit -m "Add only the feature file"
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.

bash
# DESTRUCTIVE: undo last commit AND discard all changes
git reset --hard HEAD~1

# Go back 3 commits, discarding everything
git reset --hard HEAD~3

# Reset to a specific commit SHA
git reset --hard a3b2c1d

`--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.

See this workflow in Gitoryx — Gitoryx screenshot

See this workflow in Gitoryx. Right-click any commit in the visual graph to reset, revert, or amend it — no commands to remember.

Free download
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.

bash
# Revert the most recent commit (creates a new 'undo' commit)
git revert HEAD

# Revert a specific commit by SHA
git revert a3b2c1d

# Revert without immediately committing (review first)
git revert --no-commit a3b2c1d
git revert --continue

# Revert a range of commits (exclusive of start)
git revert HEAD~3..HEAD

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

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.

bash
# Fix the commit message of the last commit
git commit --amend -m "Correct commit message"

# Add a forgotten file to the last commit
git add forgotten-file.ts
git commit --amend --no-edit

# Amend opens your editor to rewrite the message
git commit --amend

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.

Common Mistakes to Avoid

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.

Gitoryx — visual Git client for macOS, Windows & Linux
GitoryxGitoryx — macOS, Windows & Linux

Undo Git Commits Visually in Gitoryx

  • Right-click any commit in the visual graph to reset, revert, or amend it — 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.
Download Gitoryx — FreemacOS · Windows · Linux · No subscription

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 — it rewrites history. `git revert` creates a new commit that undoes the changes of a previous commit — 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.

See it in action with Gitoryx

Everything in this tutorial is faster and clearer with a visual Git client. Gitoryx is free, runs natively on macOS, Windows, and Linux, and built for developers who want to move fast without breaking things.