SoftwareSideHustles
How-To

How to Undo the Last Git Commit: reset, revert, amend and reflog

Undo a Git commit safely whether or not you have pushed it — keep or discard the changes, fix the message, remove a file, and recover from a bad reset with reflog.

By Software Side Hustles Editorial Team · Published · Updated · 5 min read

Quick answer: if you have not pushed, run git reset --soft HEAD~1 to undo the commit and keep the changes staged, or git reset HEAD~1 to keep them unstaged. If you have pushed to a shared branch, run git revert HEAD to create a new commit that reverses it. Only use git reset --hard HEAD~1 when you want to throw the changes away.

Pick the right command

SituationCommandYour changes
Wrong commit messagegit commit --amend -m "Better message"Kept, same commit replaced
Forgot a filegit add file && git commit --amend --no-editAdded to the last commit
Undo commit, keep changes stagedgit reset --soft HEAD~1Staged
Undo commit, keep changes unstagedgit reset HEAD~1 (--mixed)In working tree
Undo commit and discard changesgit reset --hard HEAD~1Deleted
Undo a commit that is already pushedgit revert HEADReversed by a new commit
"I reset and lost my work"git reflog, then git reset --hard <sha>Recovered

HEAD~1 means "one commit before the current one". Use HEAD~3 to go back three commits.

How do I undo a commit that I haven't pushed?

Because the commit only exists on your machine, you can rewrite history freely.

bash
git reset --soft HEAD~1

The commit disappears, and every change it contained is back in the staging area, ready to recommit. This is the safest form of reset — nothing is lost.

bash
git reset HEAD~1 # same as --mixed

Also removes the commit, but leaves the changes unstaged in your working tree. Use it when you want to split the work into several smaller commits.

bash
git reset --hard HEAD~1

Removes the commit and discards its changes from your working tree. Any uncommitted edits are lost as well. Run git status and git stash first if you are not completely sure.

How do I undo a commit that I already pushed?

Rewriting history that other people have pulled causes duplicated commits and confusing conflicts for them. On a shared branch, add a new commit that undoes the old one:

bash
git revert HEAD # reverse the latest commit
git revert abc1234 # reverse a specific commit
git push

git revert is safe because it only adds history. The original commit stays visible, which is usually what you want on main anyway: there is a clear record of what was undone and why.

If the branch is yours alone — a feature branch nobody else uses — you can reset locally and force-push:

bash
git reset --hard HEAD~1
git push --force-with-lease

--force-with-lease refuses to overwrite the remote if someone else pushed in the meantime, which makes it strictly safer than --force.

How do I change the last commit message?

bash
git commit --amend -m "Fix pagination off-by-one in search results"

--amend replaces the last commit with a new one. It has a new hash, so the same "already pushed?" rule applies: amending a pushed commit requires a force-push.

How do I remove a file from the last commit?

bash
git rm --cached secrets.env # stop tracking, keep the file on disk
echo "secrets.env" >> .gitignore
git commit --amend --no-edit

If the file contained a secret and you already pushed, treat the secret as compromised and rotate it. Removing it from the latest commit does not remove it from history, forks, clones or CI logs. Rewriting history with git filter-repo helps, but rotation is the real fix.

How do I recover a commit after a bad reset?

Git keeps a log of where HEAD has been, even for commits no branch points to any more:

bash
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# 9f8e7d6 HEAD@{1}: commit: Add invoice export
git reset --hard 9f8e7d6

Reflog entries are kept for a while (90 days by default for reachable entries, 30 for unreachable ones), so "lost" commits are usually recoverable. Uncommitted changes discarded by reset --hard or checkout -- file are a different story — Git never stored them. Commit or stash early.

Undoing other things

  • Unstage a file: git restore --staged file (older Git: git reset HEAD file).
  • Discard local edits to a file: git restore file. This cannot be undone.
  • Undo a merge you haven't pushed: git reset --hard ORIG_HEAD.
  • Undo a pushed merge: git revert -m 1 <merge-sha>, where -m 1 keeps the first parent (usually main).
  • Abort a merge or rebase in progress: git merge --abort or git rebase --abort. See how to resolve merge conflicts.
  • Committed to the wrong branch: git branch feature-x (keep the work), then git reset --hard HEAD~1 on the wrong branch, then git switch feature-x.

Frequently asked questions

What is the difference between git reset and git revert?

reset moves the branch pointer backwards, removing commits from the branch's history. revert adds a new commit that applies the opposite changes. Use reset for local, unpushed work; use revert for anything shared.

Does git reset delete my files?

--soft and --mixed never touch your working files. --hard overwrites them to match the target commit.

How do I undo git add?

git restore --staged <file>, or git restore --staged . for everything.

How do I undo the last N commits?

git reset --soft HEAD~N to combine them into one set of staged changes, or git revert HEAD~N..HEAD to reverse them on a shared branch.

Where this skill pays off

Choosing between reset and revert depends on context — exactly the kind of judgement AI assistants get wrong when they suggest git push --force to someone on a shared branch. Evaluating whether an AI's advice is safe, not just technically correct, is a big part of paid AI evaluation work; see common mistakes when reviewing AI-generated code.

About the author

Written and reviewed by the Software Side Hustles editorial team. Code examples are executed before publication, and platform facts link to the platform's own pages with the date they were checked.

Our editorial policy

Cookies on this site

We use essential cookies to run the questionnaire, practice labs and sign-in. With your permission we'd also like to use analytics to understand which pages help people, and advertising cookies to measure our Google Ads. Rejecting never limits any feature. Cookie policy