To undo git commit mistakes, pick the command by one question: has the commit been pushed? If it is still local, git reset --soft HEAD~1 removes the last commit and keeps its changes staged, git commit --amend rewrites it in place, and git reset --hard HEAD~1 throws it away. If it is already pushed to a shared branch, use git revert <commit>, which adds a new commit that reverses it without rewriting history.

That is the short answer. The rest of this guide covers every common case with exact commands: amending, the three reset modes, the modern git restore and git switch equivalents, reverting merges with -m 1, safe force-pushing with --force-with-lease and --force-if-includes, recovering "lost" commits from the reflog, undoing a rebase with ORIG_HEAD, and removing a secret that should never have been committed. Every command below was run in a throwaway repository on Git 2.54.0; the current release as of November 2026 is Git 2.56.1 (2026-10-26), and the commands shown here are long-standing parts of Git that behave the same on current releases.

First, Check Where You Are

Before undoing anything, look at the state of the branch and its upstream:

git status -sb
git log --oneline --graph -n 10
git log --oneline origin/main..HEAD   # commits you have that the remote does not

If origin/main..HEAD lists the commit you want to undo, it has not been pushed and you can rewrite history freely. If it does not, someone may already have it, and you should prefer git revert.

One safety habit before any destructive command: drop a bookmark.

git branch backup/before-undo

Branches cost nothing, and they make every mistake below reversible without reading the reflog.

Fix the Last Commit with git commit --amend

If the last commit has the wrong message, a missing file, or a typo, you do not need to undo it at all. Amend it:

# Change only the message
git commit --amend -m "Fix pagination off-by-one in /orders"

# Add a forgotten file, keep the message
git add src/forgotten.ts
git commit --amend --no-edit

--amend replaces the last commit with a new one (new hash), so the same rule applies as for reset: fine if unpushed, requires a force-push if pushed.

Amended the wrong thing? The previous version of the commit is still in the reflog. git reset --soft HEAD@{1} moves the branch back to the pre-amend commit and leaves the amended changes staged. We confirmed this in testing: after amending a commit named "two", git reset --soft HEAD@{1} restored "two" with the extra change sitting in the index.

How to Undo Git Commit Locally: Soft, Mixed and Hard Reset

git reset moves the current branch pointer to another commit. The mode decides what happens to the changes from the commits you "removed":

Command Branch moves back Staging area (index) Working tree Use when
git reset --soft HEAD~1 Yes Keeps changes staged Unchanged You want to recommit differently, or squash
git reset HEAD~1 (--mixed, default) Yes Unstaged Unchanged You want to re-pick which files go in the commit
git reset --hard HEAD~1 Yes Reset Overwritten You want the commit and its changes gone
git reset --keep HEAD~1 Yes Reset Keeps local edits; aborts if they conflict Drop a commit but protect uncommitted work

Undo the Last Commit and Keep the Changes

This is the most common "undo git commit" request: you committed too early, or to the wrong branch.

git reset --soft HEAD~1
git status --short
# A  b.txt
# A  c.txt

The files are still staged exactly as they were. Commit them again, or switch branches first if they belong somewhere else:

git reset --soft HEAD~1
git switch -c feature/correct-branch
git commit -m "Same change, right branch"

To squash the last five commits into one, the reset documentation gives the idiom directly: git reset --soft HEAD~5 followed by git commit.

Undo the Commit and Unstage Everything

--mixed is the default, so plain git reset HEAD~1 moves the branch back and leaves the changes as unstaged modifications or untracked files. In our test the two files committed in the undone commit came back as ?? b.txt and ?? c.txt. Use this when you want to split one commit into several with git add -p.

Throw the Commit Away

git reset --hard HEAD~1

This rewinds the branch and makes the working tree match the older commit. Files that were added in the dropped commit disappear from disk. Uncommitted edits to tracked files are overwritten too, which is the part that hurts. If you have local work you want to keep, use git reset --keep HEAD~1 instead; it refuses to run if a file you edited would be overwritten. In our test an untracked scratch file survived reset --keep untouched.

Modern Equivalents: git restore and git switch

Git 2.23 split the overloaded git checkout into two focused commands. They do not move branch pointers, so they are not substitutes for reset or revert, but they cover the "undo my uncommitted changes" half of most undo questions:

Old command Modern equivalent What it does
git checkout -- file git restore file Discard unstaged edits to a file
git reset HEAD file git restore --staged file Unstage a file, keep the edits
git checkout HEAD~3 -- file git restore --source=HEAD~3 file Bring back an older version of one file
git checkout -b name <commit> git switch -c name <commit> Create a branch at a commit, e.g. a recovered one
git checkout <commit> git switch --detach <commit> Inspect an old commit without a branch

Restoring a single file from an older commit is often a better answer than undoing the whole commit. If one file in a ten-file commit was wrong, run git restore --source=HEAD~1 path/to/file, commit the result, and leave the other nine files alone.

Undo a Pushed Commit Safely with git revert

Once a commit is on a shared branch, other people's work is built on top of it. Rewriting it forces everyone to repair their clones. git revert avoids that by creating a new commit that applies the inverse change:

git revert --no-edit a1b2c3d
git push

You can revert a range in one go, oldest-exclusive to newest-inclusive, and review before committing:

git revert --no-commit HEAD~3..HEAD
git diff --cached
git commit -m "Revert the last three commits on the import pipeline"

If a revert conflicts with later work, resolve the files, git add them, and run git revert --continue, or bail out with git revert --abort.

Reverting a Merge Commit with -m 1

A merge commit has two parents, so Git cannot know which side you want to keep. Running a plain revert fails:

error: commit 516c336... is a merge but no -m option was given.
fatal: revert failed

Tell Git which parent is the mainline. For a feature branch merged into main, parent 1 is main:

git revert -m 1 516c336

There is a trap here that the docs spell out: reverting a merge "declares that you will never want the tree changes brought in by the merge." If the feature branch is fixed and merged again later, Git considers its old commits already merged and only brings in the new ones. The fix is to revert the revert before re-merging. Git's revert-a-faulty-merge how-to walks through it.

Force-Pushing After Rewriting History

Sometimes rewriting a pushed commit is the right call: your own feature branch, a PR nobody else has checked out, or a commit that leaked a credential. Never use bare --force for this. Use --force-with-lease, which refuses to overwrite the remote branch if it has moved since you last fetched:

git push --force-with-lease origin feature/login

In our test, after a teammate pushed to the same branch, the push was rejected with ! [rejected] main -> main (stale info) instead of silently deleting their commit.

--force-with-lease has one blind spot, and the Git docs call it out: anything that runs git fetch in the background (an IDE, a cron job) updates your remote-tracking ref, so the lease check passes even though you never looked at the new commits. Add --force-if-includes (Git 2.30+), which also checks that the remote tip is reachable from your branch's reflog, meaning you actually integrated it:

git push --force-with-lease --force-if-includes origin feature/login
git config --global push.useForceIfIncludes true   # make it the default

We reproduced exactly this: after a background-style git fetch, plain --force-with-lease would have overwritten the teammate's commit (a dry run showed forced update), while adding --force-if-includes rejected the push with (remote ref updated since checkout).

Recover "Lost" Commits with git reflog

reset --hard, a botched rebase or a deleted branch does not delete commits immediately. Every move of HEAD is recorded in the reflog:

git reflog -n 10
# a3f42f4 HEAD@{0}: reset: moving to HEAD~1
# 610dd8a HEAD@{1}: commit: second again2

Find the entry before the mistake and either move the branch back or create a rescue branch:

git reset --hard HEAD@{1}            # put the branch back where it was
git switch -c rescue 610dd8a         # or keep the current branch and branch off the lost commit

How long do you have? By default, git reflog entries expire after 90 days (gc.reflogExpire), and entries for commits no longer reachable from any branch after 30 days (gc.reflogExpireUnreachable). After that, git gc can prune the objects. The reflog is also local only: a fresh clone does not have yours.

Undo a Rebase or Merge with ORIG_HEAD

Before rebase, merge, pull and reset move a branch, Git stores the old tip in ORIG_HEAD. The reset docs describe this for merges and pulls; we confirmed it for rebase too, where ORIG_HEAD held the pre-rebase tip:

git rebase main               # rewrites the branch
git reset --hard ORIG_HEAD    # back to exactly where you were

Do this immediately. Any later command that sets ORIG_HEAD (another reset, merge or rebase) overwrites it, after which you fall back to git reflog. If a rebase is still in progress and going badly, git rebase --abort is cleaner than either.

The same works for an unpushed merge: git reset --hard ORIG_HEAD right after git merge removes the merge commit.

Remove a File or Secret Committed by Mistake

A File That Should Not Be Tracked

If you committed something like .env or a build directory that is not secret, stop tracking it and ignore it going forward:

git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"

--cached removes the file from the repository but leaves your local copy on disk. The file is still in older commits; that only matters if it contains something sensitive.

If the commit is the last one and unpushed, it is simpler to amend: git rm --cached .env, then git commit --amend --no-edit.

A Secret That Was Pushed

Treat a pushed secret as compromised. GitHub's guide to removing sensitive data puts the order plainly: revoke or rotate the credential first. Rewriting history is cleanup, not remediation: forks, clones, CI caches and anyone who fetched in the meantime still have it. Our guide to removing a secret from git history covers the full procedure.

Then rewrite history with git-filter-repo, the tool Git's own documentation points to instead of git filter-branch. GitHub's guide requires filter-repo 2.47 or later for the --sensitive-data-removal flag:

# Remove a file from every commit
git filter-repo --sensitive-data-removal --invert-paths --path config/secrets.yml

# Or replace the secret's text wherever it appears
git filter-repo --sensitive-data-removal --replace-text ../replacements.txt

git push --force --mirror origin

Expect side effects: every commit from the first affected one gets a new hash, open pull requests may lose diffs, signatures are dropped, and collaborators must re-clone or rebase rather than merge (a merge reintroduces the old history). On GitHub, cached views and pull request refs need a support request to purge. Afterwards, add secret scanning to CI so it does not happen again; our guide to secretless deploys with GitHub Actions OIDC removes long-lived cloud keys from repositories altogether, and the same supply-chain thinking applies to npm trusted publishing.

Decision Table: Situation to Command

Situation Pushed? Command
Wrong message on last commit No git commit --amend -m "..."
Forgot a file in last commit No git add file && git commit --amend --no-edit
Undo last commit, keep changes staged No git reset --soft HEAD~1
Undo last commit, keep changes unstaged No git reset HEAD~1
Delete last commit and its changes No git reset --hard HEAD~1 (or --keep to protect local edits)
Committed to the wrong branch No git reset --soft HEAD~1, git switch -c right-branch, commit
Undo a bad amend No git reset --soft HEAD@{1}
Undo a commit on a shared branch Yes git revert <commit>
Undo a merged pull request Yes git revert -m 1 <merge-commit>
Undo a rebase or merge you just ran No git reset --hard ORIG_HEAD
Rewrote your own pushed branch Yes git push --force-with-lease --force-if-includes
Commit "lost" after reset or rebase Either git reflog, then git switch -c rescue <hash>
Discard uncommitted edits to a file n/a git restore file
Unstage a file n/a git restore --staged file
Pushed a secret Yes Rotate it, then git filter-repo --sensitive-data-removal, force-push

When you want to double-check exactly what an undo changed, compare the before and after versions of a file in a code difference checker, or use the git diff techniques in our guide to comparing code files.

Frequently Asked Questions

What is the difference between git reset and git revert?

git reset moves the branch pointer backwards, erasing commits from the branch's history; it is for local, unpushed work. git revert adds a new commit that undoes an earlier one, so history only moves forward; it is the safe choice for anything other people may have pulled.

How do I undo git commit but keep my changes?

Run git reset --soft HEAD~1. The commit is removed and all of its changes stay staged, ready to be committed again. Use git reset HEAD~1 instead if you want the changes unstaged.

Can I recover a commit after git reset --hard?

Usually yes. Run git reflog, find the entry from before the reset, and run git reset --hard HEAD@{n} or git switch -c rescue <hash>. Unreachable reflog entries are kept for 30 days by default. Uncommitted changes destroyed by --hard are not in the reflog and cannot be recovered this way.

How do I undo a git push?

If others may have pulled, revert the commit and push the revert. If it is your own branch and nobody else uses it, reset locally and push with git push --force-with-lease --force-if-includes. If the push contained a secret, rotate the secret first.

Why does git revert fail on a merge commit?

A merge has more than one parent, so Git needs to know which side to keep. Pass -m 1 to keep the first parent (usually the branch you merged into). Remember that a reverted merge must itself be reverted before the same branch can be merged again.

Is git restore safe to use instead of git checkout?

Yes. git restore and git switch, introduced in Git 2.23, split git checkout's two jobs into separate commands with clearer names. git restore file discards uncommitted edits permanently, so commit or stash anything you want to keep first.

Bottom Line

The safe way to undo git commit mistakes comes down to one rule: rewrite local history freely with amend and reset, and undo shared history with revert. Keep git reflog and ORIG_HEAD in mind as your safety net, force-push only with --force-with-lease --force-if-includes, and when a secret leaks, rotate it before you touch the history.